Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

android - Round buttons with icon in center and text under icon

I'm trying to do this:

enter image description here

I've tried to make customized square button:

    public class SquareButton extends Button {

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public SquareButton(Context context) {
        super(context);
    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = getMeasuredHeight();

        setMeasuredDimension(height, height);      
    }

}

and use it with icon and text under image, but without success. Button's background is a shape with gray stroke and white background color. Also there is an icon and string-text.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainMenuActivity" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1" >

            <views.SquareButton
                android:id="@+id/SquareButton01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

            <views.SquareButton
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1" >

            <views.SquareButton
                android:id="@+id/SquareButton03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

            <views.SquareButton
                android:id="@+id/SquareButton02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1" >

            <views.SquareButton
                android:id="@+id/SquareButton05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

            <views.SquareButton
                android:id="@+id/SquareButton04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

        </TableRow>
    </TableLayout>    
</LinearLayout>

Please, help. I've "googled" a lot of topics to find a solution of such problem, but not found yet. (((

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
// try this way 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:background="@android:color/holo_blue_dark">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="Main menu"/>
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_launcher"
        android:drawablePadding="5dp"
        android:text="Start learning"
        android:textColor="@android:color/holo_blue_dark"
        android:padding="5dp"
        android:gravity="center"
        android:background="@android:color/white"/>

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_launcher"
        android:drawablePadding="5dp"
        android:text="Add card"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/holo_blue_dark"
        android:padding="5dp"
        android:gravity="center"
        android:background="@android:color/white"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Import words"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Statistics"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Settings"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Change User"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:layout_marginTop="20dp"
        android:text="Hello username!"/>
</LinearLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...