0

需要明确的是,如果父linearLayout是480dp,我需要我的textview居中在80 dp位置,如果textview是10 dp宽度,那么它需要75dp到85 dp作为它的空间,我该怎么做?

4

2 回答 2

0

我相信,您可以使用对象的布局权重属性来做到这一点。例如:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/background">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="whatever" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:background="@color/background" />

</LinearLayout>

在这个例子中,View对象被用来用一些东西填充空白空间并强制TextView对象的 1/4 权重。我对我的 Android 编程有点生疏,所以我希望这对你有用。

于 2013-06-08T00:40:12.390 回答
0

您可以TextView使用 给出父级宽度的一半layout_weight,然后使用 使文本居中android:gravity

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:gravity="center" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
于 2013-06-08T04:41:49.490 回答