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%。