Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android布局权重不起作用_Android_Android Layout - Fatal编程技术网

Android布局权重不起作用

Android布局权重不起作用,android,android-layout,Android,Android Layout,我正在构建这个应用程序,只想在屏幕上平均分配这些按钮,这是我正在使用的代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orienta

我正在构建这个应用程序,只想在屏幕上平均分配这些按钮,这是我正在使用的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >


<Button
    android:id="@+id/tera_mt_serv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:onClick="teraServerBt"
    android:background="@color/gblue"
    android:textColor="#fff"
    android:text="@string/tera_server_st_mt_ab"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:layout_weight="1"
    tools:ignore="ButtonStyle" />

<Button
    android:id="@+id/tera_ff_serv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/gblue"
    android:textColor="#fff"
    android:text="@string/tera_server_st_ff_ab"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:layout_weight="1"
    tools:ignore="ButtonStyle" />

<Button
    android:id="@+id/tera_ch_serv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/gblue"
    android:textColor="#fff"
    android:onClick="teraServerBt"
    android:text="@string/tera_server_st_ch_ab"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:layout_weight="1"
    tools:ignore="ButtonStyle" />

<Button
    android:id="@+id/tera_av_serv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/gblue"
    android:textColor="#fff"
    android:text="@string/tera_server_st_av_ab"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:layout_weight="1"
    tools:ignore="ButtonStyle" />

<Button
    android:id="@+id/tera_tr_serv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/gblue"
    android:textColor="#fff"
    android:text="@string/tera_server_st_tr_ab"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:layout_weight="1"
    tools:ignore="ButtonStyle" />

但即使在我的手机上测试,我仍然得到这个结果:


我是新来的,所以这一定是我看不到的简单的东西。无论如何,提前谢谢你的帮助

将父线布局的宽度设置为与父线匹配,则按钮应平均共享全宽空间。

将父线布局的宽度设置为与父线匹配,则按钮应平均共享全宽空间。

尝试以下操作:

在您的线性布局中,设置android:weightSum=“1”并设置android:layout\u width=“match\u parent”

之后,在每个按钮上设置android:layout\u weight=“0.20”

请记住:0.20(每个布局的权重)x 5(按钮)=1个权重总和

<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="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal" >


    <Button
        android:id="@+id/tera_mt_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:onClick="teraServerBt" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_ff_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_ch_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff"
        android:onClick="teraServerBt" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_av_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_tr_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />
</LinearLayout>

试试这个:

在您的线性布局中,设置android:weightSum=“1”并设置android:layout\u width=“match\u parent”

之后,在每个按钮上设置android:layout\u weight=“0.20”

请记住:0.20(每个布局的权重)x 5(按钮)=1个权重总和

<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="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal" >


    <Button
        android:id="@+id/tera_mt_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:onClick="teraServerBt" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_ff_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_ch_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff"
        android:onClick="teraServerBt" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_av_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />

    <Button
        android:id="@+id/tera_tr_serv"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:textAlignment="center"
        android:textAllCaps="true"
        android:layout_weight="0.20"
        tools:ignore="ButtonStyle" />
</LinearLayout>


您的视图似乎没有占据全部宽度。对根视图使用匹配父宽度似乎您的视图未占用全宽度。使用根视图的匹配父宽度非常感谢!它成功了,非常感谢!它成功了,非常感谢@WildmarGomes很高兴知道。我的回答是“竖起大拇指”,对吗?:)它起作用了,非常感谢@WildmarGomes很高兴知道。我的答案是“竖起大拇指”,对吗?:)