2016-12-21 34 views
0

林建设这个应用程序,只是想在屏幕上平均分配这些按钮,这是使用的代码我真的:Android的布局重量不工作

<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" /> 

,但我仍然得到这个因此,即使我测试了我的手机上:

APP RESULT

林新在此所以它必须是简单的东西,我不能老是看。无论如何,预先感谢您的帮助!

+0

似乎您的视图不占据整个宽度。使用match_parent宽度作为根视图 – Rahul

回答

2

将您的父LinearLayout的宽度设置为match_parent,然后您的按钮应平均分享全宽的空间。

+0

它工作非常感谢! –

0

尝试这样的:

在您的LinearLayout设置android:weightSum="1"并设置android:layout_width="match_parent"

在此之后,在每个按钮上设置android:layout_weight="0.20"

记住:0.20(每个layout_weight)×5(按钮)= 1 weightSum

<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> 
+1

非常感谢! –

+0

@WildmarGomes很高兴知道。 “竖起大拇指”为我的答案,对吧?:) –