2011-10-23 73 views
1

我的问题很简单。我有布局:如何设置元素的宽度百分比?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@drawable/mobile_background" 
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:layout_height="fill_parent" 
    > 
    <Button 
     android:text="GET MESSAGES" 
     android: 
     android:id="@+id/buttonMessages" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:text="REGISTRATION" 
     android:id="@+id/buttonRegistration" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:text="SETTINGS" 
     android:id="@+id/buttonSettings" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

但我需要设置非固定大小的按钮 - 我需要设置尺寸屏幕宽度的30%。我该怎么做?

回答

1

尝试将水平LinearLayouts中的按钮与另一视图嵌套到侧边进行填充。分配你的按钮并查看android:layout_width="0dp",然后给你的按钮android:layout_weight="3"和视图android:layout_weight="7"。这将使您的按钮30%和空视图70%。

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

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

     <Button 
      android:id="@+id/buttonMessages" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="GET MESSAGES" /> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="7" /> 
    </LinearLayout> 

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

     <Button 
      android:id="@+id/buttonRegistration" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="REGISTRATION" /> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="7" /> 
    </LinearLayout> 

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

     <Button 
      android:id="@+id/buttonSettings" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="SETTINGS" /> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="7" /> 
    </LinearLayout> 

</LinearLayout> 
+0

还有什么意思呢? – user975290

+0

您可以使用'findViewById'在您的活动中动态设置它们的宽度,但我认为xml方法并不令人头疼。 – Craigy

0

三个按钮,很难设置30-70 :)我会告诉你如何有两个做它,你就会得到它。

如果对宽度使用fill_parent,则空间将以相反的比例分布。所以你应该为较小的按钮设置7的权重,为较大的按钮设置3。使用更多的按钮会更棘手,但您可以进行数学运算:1/x + 1/y + 1/z = 1,在30%,20%和50%的情况下,您将得到比例方程式: 1/X/3 = 1/Y/2 = 1/Z/5或者你可以只是尝试和设定值,看起来不错:)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@drawable/mobile_background" 
    android:layout_width="fill_parent" 
    android:gravity="center"/> 
    <Button 
     android:text="Narrow button" 
     android: 
     android:id="@+id/buttonMessages" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="7"/> 
    <Button 
     android:text="Wide button" 
     android:id="@+id/buttonRegistration" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="3"/> 
</LinearLayout> 
0

这不能在XML完成。你将不得不在代码中指定大小。我会做的是,在活动的onCreate():

obj.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,0.25)); 

这意味着“使用父的高度的100%,但母公司的宽度的25%”。

source

相关问题