Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何布局textview,使其位于父linearLayout的1/4位置的中心?_Android_Layout_Textview_Android Linearlayout - Fatal编程技术网

Android 如何布局textview,使其位于父linearLayout的1/4位置的中心?

Android 如何布局textview,使其位于父linearLayout的1/4位置的中心?,android,layout,textview,android-linearlayout,Android,Layout,Textview,Android Linearlayout,要明确的是,如果父linearLayout是480dp,我需要我的textview在80dp位置居中,如果textview是10dp宽度,那么它需要75dp到85dp作为它的空间,我该怎么做呢?我相信,你可以使用对象的布局权重属性来做这件事。例如: <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@col

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

我相信,你可以使用对象的布局权重属性来做这件事。例如:

<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>


在本例中,视图对象用于用某物填充空白,并强制执行文本视图对象的1/4权重。我对我的Android编程有点生疏,所以我希望这对你有用。

你可以使用
布局权重给
文本视图提供父视图宽度的一半,然后用
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>


不,这不起作用,我的意思是文本视图应该在80 dp的位置居中,这意味着它可能需要75到85的空间。啊,我明白了。我的错误。我现在就把这个留在这里,这样其他人就不会给出同样的答案了。