我已经通过大量类似的问题,看起来在这里SO,但没有什么帮助。我有不同的嵌套布局的层次,都有着android:layout_width="fill_parent"
,和最内层的布局具有android:layout_width="wrap_content
- 我需要在中心(水平)对齐。我怎么做?
更新::我已经找到了问题的原因 - 如果我把内心的LinearLayout到的RelativeLayout与android:layout_width="fill_parent"
,它仍然包裹它的内容。本的TableRow,然而,真的是填充屏幕。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
这两个属性通常困惑:
android:gravity
将它用在浏览内容的严重性。android:layout_gravity
设置视图或布局相对于其父的严重性。所以,要么把android:gravity="center"
上的的LinearLayout本身的父母或android:layout_gravity="center"
。
我捉住了我多次把它们组合起来,不知道为什么事情不正确中心...
这为我工作。
<LinearLayout>
.
.
.
android:gravity="center"
.
.>
<TextView
android:layout_gravity = "center"
/>
<Button
android:layout_gravity="center"
/>
</LinearLayout>
所以你在设计的线性布局,以将其全部内容(的TextView和Button)在它的中心,然后TextView的和按钮相对于放置到线性布局的中心
我经历过这种同时使用嵌套RelativeLayouts工作。我的解决方案最终是简单,但它是一个小的疑难杂症是值得意识到的。
我的布局与定义...
android:layout_height="fill_parent"
android:layout_below="@id/someLayout
android:gravity="center"
......不过,我没想到,在这之下的布局被放置在它的上面。这种布局内的一切都是朝着底部移动,而不是居中。添加...
android:layout_above="@id/bottomLayout"
...固定我的问题。
为ImageView的或别人的意见谁布局重力或重力不存在使用:在孩子android:layout_centerInParent="true"
(我有一个ImageView的作为一个孩子一个相对布局)。
问题是根本的(主要布局在您存储其他元素)比重,没有设置。如果您将其更改为中心的其他元素重力必须工作得很好。
下面的代码把你的LinearLayout在底部,并把它的内容的中心。你必须使用的RelativeLayout在底部设置布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#97611F"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="View Saved Searches"
android:textSize="15dp"
android:textColor="#fff"
android:gravity="center_vertical"
android:visibility="visible" />
<TextView
android:layout_width="30dp"
android:layout_height="24dp"
android:text="12"
android:textSize="12dp"
android:textColor="#fff"
android:gravity="center_horizontal"
android:padding="1dp"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
请将您的线性布局试试这个
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</RelativeLayout>
考虑包装的RelativeLayout过的LinearLayout。 android:layout_centerHorizontal="true"
将定位视图中心水平。
添加layout_gravity="center"
或"center_horizontal"
父布局。
在一个侧面说明,你的LinearLayout
里面你TableRow
似乎非必要,作为TableRow
已经是一个水平LinearLayout
。
尝试这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" >
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
这个工作对我..加入空观点..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
尝试<TableRow android:gravity="center_horizontal">
这将tablerow的内中心内的LinearLayout。
在设计自定义通知我曾经面临同样的情况。我曾尝试使用以下属性与真正的设置。
android:layout_centerInParent.
@jksschneider explation差不多吧。请确保您未设置任何gravity
于母公司的布局,然后layout_gravity="center"
设为您的视图或布局。