均匀间距按钮android

时间:2017-03-13 11:36:05

标签: java android xml

我正在开发一个Android应用程序。我无法在屏幕上均匀分布按钮。

以下是我的代码。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_menu"
    android:background="#354739"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="com.example.cssdapp.MainMenuActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"
        />

    <Button
        android:text="Log Off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#6b5847"
        android:id="@+id/logOff"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="Manage Farmers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#6b5847"
        android:id="@+id/button4"
        android:layout_above="@+id/button5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="My Account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#6b5847"
        android:id="@+id/button5"
        android:layout_marginBottom="47dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="Manage Farmers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#6b5847"
        android:id="@+id/button3"
        android:layout_above="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="21dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="Order History"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#6b5847"
        android:id="@+id/button2"
        android:layout_above="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="Make an Order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#6b5847"
        android:id="@+id/makeOrder"
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="36dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

我希望按钮在图像下方的屏幕上均匀分布。在那一刻,图像是我想要的地方,但是我无法将按钮移到我想要的位置。我正在尝试使用gui设计师但是他们不会碰到位置。我正试图找出代码的方法来实现它。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:1)

为了均匀划分整个屏幕上的按钮并在所有设备上保持同步,您需要使用LinearLayout 作为您的父视图和另一个LinearLayout作为每个按钮的父级并应用它的权重属性将它们均匀地划分在屏幕上因为您不能只在按钮本身上应用权重属性,因为它的高度或宽度取决于布局orintation是水平还是垂直将缩放以填充整个空间)这里是一个代码片段来演示我在说什么 `

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_menu"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

        <Button
            android:id="@+id/logOff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Log Off"
            android:textAllCaps="false"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button4"
            android:text="Manage Farmers"
            android:textStyle="bold"
            android:textAllCaps="false"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:text="My Account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button5"
            android:textAllCaps="false"
            android:textStyle="bold"/>
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:text="Order History"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:textAllCaps="false"
            android:textStyle="bold"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:text="Make an Order"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/makeOrder"
            android:textAllCaps="false"
            android:textStyle="bold"/>
    </LinearLayout>
</LinearLayout>`

答案 1 :(得分:0)

I have done some changes, Please check if it works for you

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#354739"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp"
        android:src="@drawable/ic_android_black_24dp"/>

    <Button
        android:id="@+id/logOff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv"
        android:layout_marginTop="23dp"
        android:background="#6b5847"
        android:text="Log Off"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button5"
        android:layout_marginTop="23dp"
        android:background="#6b5847"
        android:text="Manage Farmers"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logOff"
        android:layout_marginTop="23dp"
        android:background="#6b5847"
        android:text="Manage Farmers"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:layout_marginTop="23dp"
        android:background="#6b5847"
        android:text="My Account"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button5"
        android:layout_marginTop="23dp"
        android:background="#6b5847"
        android:text="Order History"/>

    <Button
        android:id="@+id/makeOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_marginTop="23dp"
        android:background="#6b5847"
        android:text="Make an Order"/>
</RelativeLayout>
相关问题