Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.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 如何将视图居中对齐?_Android_Android Layout - Fatal编程技术网

Android 如何将视图居中对齐?

Android 如何将视图居中对齐?,android,android-layout,Android,Android Layout,我需要像在LinearLayout中一样,将数量可变的视图(可能只有一个)彼此相邻放置。但我希望整个安排是中心对齐的。视图应该相邻。但是,整个排列应该与屏幕的左右边缘或包含的父对象等距。我怎样才能做到这一点呢?你试过了吗 android:gravity="center" ?这会帮你的 android:layout\u gravity=“居中水平” 也要考虑权重属性以确保这个布局元素比其他元素优先。 要将所有内容组合在一起,可以使用框架布局或相对布局。您必须将视图包装在线性布局中

我需要像在LinearLayout中一样,将数量可变的视图(可能只有一个)彼此相邻放置。但我希望整个安排是中心对齐的。视图应该相邻。但是,整个排列应该与屏幕的左右边缘或包含的父对象等距。我怎样才能做到这一点呢?

你试过了吗

      android:gravity="center" 

这会帮你的

android:layout\u gravity=“居中水平”

也要考虑权重属性以确保这个布局元素比其他元素优先。


要将所有内容组合在一起,可以使用框架布局或相对布局。

您必须将视图包装在
线性布局中,将线性布局包装在其他布局中:

<LinearLayout
    android:orientation="vertical">
    <LinearLayout
       android:layout_width="wrap_content"
       android:layout_gravity="center_horizontal">
       <View/>
       <View/>
       etc...
    </LinearLayout>
</LinearLayout>
<RelativeLayout>
    <LinearLayout
       android:layout_width="wrap_content"
       android:layout_centerHorizontal="true">
       <View/>
       <View/>
       <View/>
    </LinearLayout>
</RelativeLayout>