当前位置: 动力学知识库 > 问答 > 编程问答 >

Four straight same scaled buttons in android

问题描述:

I need a sample layout with 4 same size buttons and for same margins from every side (propably relative layout). Just want them to looks nice and straight. I tried something like this:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/RelativeLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<LinearLayout

android:id="@+id/linearLayout2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/linearLayout3"

android:layout_toRightOf="@+id/linearLayout3"

android:orientation="vertical" >

<Button

android:id="@+id/buttonMainFindInteractions"

android:layout_width="152dp"

android:layout_height="91dp"

android:layout_marginLeft="30dp"

android:drawableTop="@android:drawable/ic_menu_search"

android:text="@string/findInteraction"

android:textSize="12sp" />

</LinearLayout>

<LinearLayout

android:id="@+id/linearLayout4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignRight="@+id/linearLayout2"

android:layout_below="@+id/linearLayout2"

android:layout_marginTop="54dp"

android:orientation="vertical" >

<Button

android:id="@+id/buttonMainCheckMap"

android:layout_width="152dp"

android:layout_height="91dp"

android:drawableTop="@android:drawable/ic_menu_preferences"

android:text="@string/settings"

android:textSize="12sp" />

</LinearLayout>

<LinearLayout

android:id="@+id/linearLayout3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginTop="80dp"

android:orientation="vertical" >

<Button

android:id="@+id/buttonMainFindReplacements"

android:layout_width="152dp"

android:layout_height="91dp"

android:drawableTop="@android:drawable/ic_menu_search"

android:text="@string/find_replacement"

android:textSize="12sp" />

</LinearLayout>

<LinearLayout

android:id="@+id/linearLayout1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignTop="@+id/linearLayout4"

android:orientation="vertical" >

<Button

android:id="@+id/buttonMainSettings"

android:layout_width="152dp"

android:layout_height="91dp"

android:drawableTop="@android:drawable/ic_menu_search"

android:text="@string/checkMap"

android:textSize="12sp" />

</LinearLayout>

</RelativeLayout>

But I can't find the way to size those 4 buttons in the same way.

2nd question: why inside of activity im looking for:

findReplacementsButton = (Button) this.findViewById(R.id.buttonMainFindReplacements);

It returns me a linearlayout? This does not make sense..

Thanks for help!

网友答案:

Try this xml:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"

    >

    <Button
        android:id="@+id/buttonMainFindInteractions"
        android:layout_width="152dp"
        android:layout_height="91dp"
        android:drawableTop="@android:drawable/ic_menu_search"
        android:text="@string/findInteraction"
        android:textSize="12sp" />

    <Button
        android:layout_toRightOf="@+id/buttonMainFindInteractions"
        android:layout_alignBottom="@+id/buttonMainFindInteractions"
        android:id="@+id/buttonMainCheckMap"
        android:layout_width="152dp"
        android:layout_height="91dp"
        android:drawableTop="@android:drawable/ic_menu_preferences"
        android:text="@string/settings"
        android:textSize="12sp" />


    <Button
        android:layout_below="@+id/buttonMainFindInteractions"
        android:id="@+id/buttonMainFindReplacements"
        android:layout_width="152dp"
        android:layout_height="91dp"
        android:drawableTop="@android:drawable/ic_menu_search"
        android:text="@string/find_replacement"
        android:textSize="12sp" />


    <Button
        android:layout_alignBottom="@+id/buttonMainFindReplacements"
        android:layout_toRightOf="@+id/buttonMainFindReplacements"
        android:id="@+id/buttonMainSettings"
        android:layout_width="152dp"
        android:layout_height="91dp"
        android:drawableTop="@android:drawable/ic_menu_search"
        android:text="@string/checkMap"
        android:textSize="12sp" />

</RelativeLayout>

分享给朋友:
您可能感兴趣的文章:
随机阅读: