簡體   English   中英

均勻間隔按鈕android

[英]Evenly spacing buttons android

我目前正在開發一個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設計器,但是它們不會固定到位。 我正在嘗試找出執行此操作的代碼方式。

任何幫助表示贊賞。

為了在整個屏幕上平均划分按鈕並在所有設備上保持同步,您需要使用LinearLayout作為父視圖,並使用另一個LinearLayout作為每個按鈕的父視圖,並將其權重屬性應用於其上,以在屏幕上平均划分它們屏幕上的原因因為您不能僅僅在按鈕本身上應用weight屬性,因為(它的高度或寬度取決於布局的方向是水平還是垂直,將縮放以填充整個空間),這里是一個代碼段,用於演示在說什么

<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>`
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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM