【发布时间】:2013-06-04 09:22:10
【问题描述】:
要清楚,如果父linearLayout是480dp,我需要我的textview居中在80 dp的位置,如果textview是10 dp宽度,那么它需要75dp到85 dp作为它的空间,我该怎么做?
【问题讨论】:
标签: android layout textview android-linearlayout
要清楚,如果父linearLayout是480dp,我需要我的textview居中在80 dp的位置,如果textview是10 dp宽度,那么它需要75dp到85 dp作为它的空间,我该怎么做?
【问题讨论】:
标签: android layout textview android-linearlayout
我相信,您可以使用对象的 layout weight 属性来做到这一点。例如:
<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 编程有点生疏,所以我希望这对你有用。
【讨论】:
您可以使用layout_weight 将TextView 赋予父级宽度的一半,然后使用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>
【讨论】: