例如我希望按钮的边距是屏幕宽度的1/4(以像素为单位),我该怎么办?我更喜欢在布局xml文件中更改某些内容而不是编码。
答案 0 :(得分:2)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4" >
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
答案 1 :(得分:0)
使用weightSum
和layout_weight
来实现此目标。
例如
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
android:visibility="visible" >
<Button
android:id="@+id/sign_in_button"
android:layout_weight="25"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="testing" />
</LinearLayout>