以内容为中心的Android

以内容为中心的Android,android,android-layout,Android,Android Layout,我完全是Android布局的初学者,但我想垂直和水平地将内容放在中心位置 所以我正在读的这本书说: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/background" android:layout_height="match_parent" android:layout_width="match_parent"

我完全是Android布局的初学者,但我想垂直和水平地将内容放在中心位置

所以我正在读的这本书说:

 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@color/background"
   android:layout_height="match_parent"
   android:layout_width="match_parent"
   android:padding="30dip"
   android:orientation="horizontal" >
   <LinearLayout
     android:orientation="vertical"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:layout_gravity="center" >
     ...component.. etc...

…组件。。等
这是可行的,我想我明白了,除了Eclipse说:

此线性布局或其父级是无用的;转移背景 属性到另一个视图


但我不明白这一点?

在两个线性布局中添加
android:background
参数

此线性布局或其父级是无用的;转移背景 属性到另一个视图

当任何
版面
只有一个子版面(也是
版面
时,会触发此警告。在您的情况下,
LinearLayout
。在这些情况下,可以毫无问题地移除其中一个。建议删除这些冗余布局。它是。。正如它所说的只是一个警告,它不会导致任何异常,但如果您删除冗余布局,将有助于提高整体性能

此线性布局或其父级是无用的;转移背景 属性到另一个视图

意味着您可以通过父级it自身管理整个布局,您不必要地添加另一个
LinearLayout
,这会降低布局性能

您可以使用
android:gravity=“center”
而不是子系统提供给父系统的
android:layout\u gravity=“center”

解决方案

您可以将线性布局组合为

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@color/background"
   android:orientation="vertical"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent"
    android:gravity="center" 
   android:padding="30dip"
   >

你应该分享你的全部代码,这样每个人都能了解你问题的整个情况。任何一种方式你都可以按照三种方式去做

方法一:

android:layout\u centerInParent=“true”

以上代码将使您的布局水平和垂直居中

方法二:

如果你想单独做(有时可能需要)

android:layout\u centerHorizontal=“true”

android:layout\u centerVertical=“true”

方法三:

android:gravity=“center”(虽然我看到您使用了。可能是您遗漏了代码中的任何一点)


希望这对你有帮助。愉快的编码。

发布您的整个xml文件。RelativeLayout有很多内容。但是为什么线性布局中的线性布局。OIt不会严重影响应用程序性能我觉得您的Answare是最好的。因为我必须感觉到我应该合并成二比一,我最初认为是这样的,但后来背景变成了半可见。另一方面,这个解决方案非常有效!我真的不明白重力=中心的作用以及它为什么起作用。您能进一步解释一下吗?父视图中的android:gravity=“center”将使其子视图与中心对齐&子视图中的android:layout\u gravity=“center”将使其与父视图的中心对齐……谢谢您的解答;)很高兴帮助你@Grimm