【问题标题】:LinearLayout Center线性布局中心
【发布时间】:2011-03-20 19:26:43
【问题描述】:

LinearLayout 的对齐有点问题。

我试图让前两个元素左对齐,第三个元素位于屏幕中心。

这是我的代码(从 id、text、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>

alt text http://img807.imageshack.us/img807/5953/imageg.png

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

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

有什么想法吗?

【问题讨论】:

    标签: android center android-linearlayout


    【解决方案1】:

    所以你需要使用的代码如下

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

    【讨论】:

      【解决方案2】:

      使用RelativeLayout 而不是LinearLayout。拥有粉红色只是一个正常的孩子。有黄色使用android:layout_toRightOf 把它放在粉红色的右边。有红用android:layout_centerHorizontal="true"

      【讨论】:

        猜你喜欢
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 2013-07-08
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多