Android 如何创建三个等距按钮

Android 如何创建三个等距按钮,android,android-layout,Android,Android Layout,我是android开发新手,我只是想知道如何在屏幕底部创建三个等距按钮。我还想知道我可以用什么风格使这些按钮看起来整洁 到目前为止我编写的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"

我是android开发新手,我只是想知道如何在屏幕底部创建三个等距按钮。我还想知道我可以用什么风格使这些按钮看起来整洁

到目前为止我编写的代码

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/Fragment1"
                android:id="@+id/worldTextView" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <Button
            android:id="@+id/Button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Button1"
           />

            <Button
                android:id="@+id/Button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Button2"

                 />

            <Button
             android:id="@+id/Button3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/Button3"
            />
   </LinearLayout>


将三个按钮保持在底部不同的布局中(方向水平的线性布局),并将每个按钮的权重和布局宽度设置为零。

将三个按钮保持在不同的布局中(方向水平的线性布局)在底部,为每个按钮赋予权重,布局宽度为零。

您应该为所有三个按钮使用
android:layout\u weight

 <Button
   android:id="@+id/Button1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/Button1"
   android:layout_weight="1"/>

您应该为所有三个按钮使用
android:layout\u weight

 <Button
   android:id="@+id/Button1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/Button1"
   android:layout_weight="1"/>


使用相对布局。。渲染xml布局文件时,线性布局过多会导致负担过重。

使用相对布局。。渲染xml布局文件时,过多的线性布局将导致负担过重。

请尝试这种方式。

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <Button
            android:id="@+id/Button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/Button1"
            android:layout_weight="1"
           />

            <Button
                android:id="@+id/Button2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/Button2"
                android:layout_weight="1"
                 />

            <Button
             android:id="@+id/Button3"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:text="@string/Button3"
             android:layout_weight="1"
            />
   </LinearLayout>

试试这个方法

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <Button
            android:id="@+id/Button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/Button1"
            android:layout_weight="1"
           />

            <Button
                android:id="@+id/Button2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/Button2"
                android:layout_weight="1"
                 />

            <Button
             android:id="@+id/Button3"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:text="@string/Button3"
             android:layout_weight="1"
            />
   </LinearLayout>

给LinearLayout一个weightSum属性及其子属性赋值layout\u weight属性,如下面的代码所示

然后给出父级和子级匹配的高度和宽度\u父级
这将允许布局根据屏幕分辨率和父级布局高度和宽度调整高度和宽度

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum = "3">
    <Button
        android:id="@+id/Button1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/Button1"
       />

        <Button
            android:layout_weight="1"
            android:id="@+id/Button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/Button2"

             />

        <Button
         android:layout_weight="1"
         android:id="@+id/Button3"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="@string/Button3"
        />


给LinearLayout一个weightSum属性及其子属性赋值layout\u weight属性,如下面的代码所示

然后给出父级和子级匹配的高度和宽度\u父级这将允许布局根据屏幕分辨率和父级布局高度和宽度调整高度和宽度

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum = "3">
    <Button
        android:id="@+id/Button1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/Button1"
       />

        <Button
            android:layout_weight="1"
            android:id="@+id/Button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/Button2"

             />

        <Button
         android:layout_weight="1"
         android:id="@+id/Button3"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="@string/Button3"
        />


要使按钮间距均匀,您可以将每个按钮
android:layout\u width
设置为0,将其
android:layout\u weight
设置为1,使每个按钮的宽度相同

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
    <Button
        android:id="@+id/Button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Button1"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/Button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Button2" 
        android:layout_weight="1"/>
    <Button
        android:id="@+id/Button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Button3"
        android:layout_weight="1"/>
</LinearLayout>

您的线性布局还需要一个非零且不依赖于其子级的
布局宽度
(例如
包裹内容
)。在本例中,我使用了
fill\u parent
,但也可以使用
match\u parent
或硬编码值,如
55dp


这是一本关于如何使用android:layout\u weight的深入指南。要使按钮间距均匀,您可以将每个按钮的宽度设置为0,将其设置为1,使每个按钮的宽度相同

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
    <Button
        android:id="@+id/Button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Button1"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/Button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Button2" 
        android:layout_weight="1"/>
    <Button
        android:id="@+id/Button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Button3"
        android:layout_weight="1"/>
</LinearLayout>

您的线性布局还需要一个非零且不依赖于其子级的
布局宽度
(例如
包裹内容
)。在本例中,我使用了
fill\u parent
,但也可以使用
match\u parent
或硬编码值,如
55dp


这是一本关于使用
android:layout\u weight

使用
android:layout\u width=“0dp”
android:layout\u weight=“1”
并设置线性布局的宽度
android:layout\u width=“match\u parent”


快照

编辑:

也可以使用相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" 
            android:layout_height="fill_parent"
             >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Fragment1" />

    <LinearLayout
                android:layout_width="match_parent" 
                android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                android:orientation="horizontal">
            <Button
                android:id="@+id/Button1"
                android:layout_width="0dp" 
                android:layout_weight="1"  
                android:layout_height="wrap_content"
                android:text="Button1"
               />

                <Button
                    android:id="@+id/Button2"
                     android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Button2"

                     />

                <Button
                 android:id="@+id/Button3"
                  android:layout_weight="1"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button3"
                />
       </LinearLayout>


       </RelativeLayout>

使用
android:layout\u width=“0dp”
android:layout\u weight=“1”
并设置线性布局的宽度
android:layout\u width=“match\u parent”


快照

编辑:

也可以使用相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" 
            android:layout_height="fill_parent"
             >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Fragment1" />

    <LinearLayout
                android:layout_width="match_parent" 
                android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                android:orientation="horizontal">
            <Button
                android:id="@+id/Button1"
                android:layout_width="0dp" 
                android:layout_weight="1"  
                android:layout_height="wrap_content"
                android:text="Button1"
               />

                <Button
                    android:id="@+id/Button2"
                     android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Button2"

                     />

                <Button
                 android:id="@+id/Button3"
                  android:layout_weight="1"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="Button3"
                />
       </LinearLayout>


       </RelativeLayout>


将高度设置为
匹配父项
?为什么需要三个按钮覆盖整个屏幕?同时将高度设置为
match\u parent
?为什么需要三个按钮覆盖整个屏幕?