Android相对布局与按钮宽度使用权重。

9 浏览
0 Comments

Android相对布局与按钮宽度使用权重。

我使用了layout_weight参数来将按钮的宽度设置为总布局宽度的70%,但似乎我错过了一些重要的细节,以使其起作用。\n(另一个解决方案是通过display.getWidth()进行编程,但它也不起作用,因为我不知道如果选择使用button.setWidth()来设置宽度,我的.xml应该是什么样的)\n



    
    
        
           
    

0
0 Comments

在使用RelativeLayout时,可能无法使用layout_weight属性。可以尝试在RelativeLayout内部添加一个LinearLayout,并在其中使用layout_weight属性。

另外,在使用layout_weight属性时,通常需要将对象的宽度或高度定义为0dp,因此在这种情况下,可以这样设置:

android:layout_weight="0.7"

android:layout_height="0dp"

0
0 Comments

这段代码中,使用了RelativeLayout布局,其中包含了多个TextView和Button。问题是第一个按钮的大小比第二个按钮小一些。猜测可能是文本的大小导致的。

要使两个按钮的大小完全相同,即都是屏幕宽度的70%,只需将按钮的宽度参数改为android:layout_width="0dp"。

虽然这个答案是有效的,但是代码本身并没有解释清楚。下面是一个更好的答案,包括代码和详细的解释为什么有效。

Android相对布局中使用权重设置按钮宽度的问题

这段代码中,使用了RelativeLayout布局,其中包含了多个TextView和Button。问题是第一个按钮的大小比第二个按钮小一些。猜测可能是文本的大小导致的。

要使两个按钮的大小完全相同,即都是屏幕宽度的70%,只需将按钮的宽度参数改为android:layout_width="0dp"。

下面是修改后的代码:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="15px"  
                android:id="@+id/userVersionTextViewNew"
                android:gravity="center"
                android:layout_centerVertical="true"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="15px"
                android:gravity="center"
                android:layout_above="/userVersionTextViewNew"
                android:id="@+id/userSoftSerialNumberTextView"/>
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="/logo_200"
                android:layout_above="/userSoftSerialNumberTextView"
                android:layout_centerHorizontal="true"/>    
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="15px"
                android:gravity="center"
                android:layout_below="/userVersionTextViewNew"
                android:id="@+id/dummyTextView"/>      
            <LinearLayout  
                android:layout_height="wrap_content"  
                android:layout_width="fill_parent" 
                android:gravity = "center_horizontal"
                android:layout_below="/dummyTextView"
                android:id="@+id/loginButtonLayout"
                android:weightSum="1.0">  
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/loginButton"
                    android:text="Σύνδεση"
                    android:layout_weight="0.7"/>
            </LinearLayout>
            <LinearLayout  
                android:layout_height="wrap_content"  
                android:layout_width="fill_parent" 
                android:gravity = "center_horizontal"
                android:layout_below="/loginButtonLayout"
                android:weightSum="1.0">  
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/demoLoginButton"
                    android:text="Δοκιμαστική χρήση"
                    android:layout_weight="0.7"/>
            </LinearLayout>
        </RelativeLayout>
    

通过将按钮的宽度设置为0dp,使用权重来控制按钮的宽度,确保两个按钮的宽度相同,并且都是屏幕宽度的70%。

0
0 Comments

问题:Android相对布局中使用按钮宽度的权重

原因:相对布局不能使用layout_weight参数,这是LinearLayout的参数。

解决方法:使用LinearLayout,可以在一行中使用权重分配来定位元素。在添加layout_weight时,不要忘记使用0dp宽度。以下示例显示了70/30的权重分配。


    

解释:每当开始设计一个具有多个元素的布局时,建议使用RelativeLayout而不是LinearLayout。RelativeLayout非常强大,可以让您根据其他元素的位置来定位元素(leftOf,below等)。在大多数情况下,这已经足够了。

LinearLayout是一个看起来也很强大的布局,但是为了只使用LinearLayout来排序所有内容,您很可能会开始嵌套这些布局。从性能角度来看,这是不好的。



    
    
    
    



    
    
    
    

0