Android 中是否有百分比宽度的选项?我试图将我的 2 行设置为 40%,将文本设置为 20%,但我真的不知道如何实现。我现在有固定宽度。
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="14dp"
android:id="@+id/line2"
android:background="@drawable/line"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/line" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/or"
android:textAllCaps="true"
android:gravity="center"
android:id="@+id/textView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:background="@drawable/line"
android:layout_gravity="center_horizontal"
android:layout_weight="2.18"
android:contentDescription="@string/line" />
</LinearLayout>
为了更好地理解,这是来自 android studio 的屏幕:screen
最佳答案
您可以为此使用属性 android:layout_weight
。
像这样:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="14dp"
android:id="@+id/line2"
android:background="@drawable/line"
android:layout_gravity="center_horizontal"
android:layout_weight="2"
android:contentDescription="@string/line" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/or"
android:textAllCaps="true"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/textView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:background="@drawable/line"
android:layout_gravity="center_horizontal"
android:layout_weight="2"
android:contentDescription="@string/line" />
</LinearLayout>
请注意,我已经为 LinearLayout
下的每个元素添加了一个权重,并且我已经将 LinearLayout
本身中的 android:weightSum
更改为5.
关于android - 线性布局中线条的宽度百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930482/