线性布局居中对齐

4

我在LinearLayout中遇到了一点对齐的问题。

我想让前两个元素左对齐,第三个元素居中于屏幕。

这是我的代码(已删除id、文本和src):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/color_background"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">
        </TextView>
    </LinearLayout>
</LinearLayout>

替代文本 http://img807.imageshack.us/img807/5953/imageg.png

这是我想做的事情,左边是粉色和黄色,中间是红色。

pink = imageview
yellow = 1st texview
red = 2nd textview

有任何想法吗?
2个回答

6
所以您需要使用的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/image"    
        android:layout_height="wrap_content"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

1
使用RelativeLayout代替LinearLayout。将粉色视为普通子元素。使用android:layout_toRightOf将黄色放置在粉色右侧。使用android:layout_centerHorizontal="true"将红色居中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接