【问题标题】:How can I set width of element in percent?如何以百分比设置元素的宽度?
【发布时间】:2011-10-23 19:45:19
【问题描述】:

我的问题很简单。我有布局:

<?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%。我该怎么做?

【问题讨论】:

    标签: java android


    【解决方案1】:

    尝试将您的按钮嵌套在水平线性布局中,并将另一个视图放在一边以进行填充。分配您的按钮和视图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>
    

    【讨论】:

    • 还有什么别的办法吗?
    • 您可以使用 findViewById 在您的活动中动态设置它们的宽度,但我认为 xml 方法不那么令人头疼。
    【解决方案2】:

    三个按钮很难设置 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>
    

    【讨论】:

      【解决方案3】:

      这不能在 XML 中完成。您必须在代码中指定大小。我会在活动的 onCreate() 中这样做:

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

      这意味着“使用 100% 的父级高度但 25% 的父级宽度”。

      source

      【讨论】:

        猜你喜欢
        • 2012-04-22
        • 2013-03-24
        • 2012-10-03
        • 2011-08-05
        • 2013-05-25
        • 2013-01-22
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多