Android:如何使按钮的边距相对于屏幕宽度?

时间:2013-01-28 12:08:56

标签: java android xml

例如我希望按钮的边距是屏幕宽度的1/4(以像素为单位),我该怎么办?我更喜欢在布局xml文件中更改某些内容而不是编码。

2 个答案:

答案 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)

使用weightSumlayout_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>