首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何布局?使用相对布局还是线性布局?

如何布局?使用相对布局还是线性布局?
EN

Stack Overflow用户
提问于 2013-04-08 22:39:28
回答 3查看 361关注 0票数 0

我正在为一个Android布局而苦苦挣扎,这看起来应该是如此简单,但没有joy,一个简单的线性布局结果如图所示:

代码语言:javascript
复制
<?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="#ffffff"
    android:minWidth="25px"
    android:minHeight="100dp">
    <TextView
        android:text="Last Game"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:gravity="center_horizontal"
        android:background="#003300" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="80dp"
            android:id="@+id/imageView1"
            android:layout_gravity="left"
            android:layout_height="80dp" />
        <TextView
            android:textColor="#000000"
            android:text="WR"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textView2"
            android:gravity="right"
            android:paddingRight="5dp" />
        <View
            android:background="#000000"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:id="@+id/view1"
            android:layout_gravity="center_horizontal" />
        <TextView
            android:textColor="#000000"
            android:text="LR"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textView3"
            android:gravity="left"
            android:paddingLeft="5dp" />
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_gravity="right"
            android:layout_width="80dp"
            android:id="@+id/imageView1"
            android:layout_height="80dp" />
    </LinearLayout>
</LinearLayout>

我想要的是左图像在左边缘(左对齐),右图像在右边缘(右对齐),垂直条始终居中,WR在靠近中心的地方右对齐,LR在靠近中心的地方左对齐。

我应该使用相对布局吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-08 22:46:41

我会的。您可以使用

代码语言:javascript
复制
android:layout_alignParentLeft="true"

代码语言:javascript
复制
android:layout_alignParentRight="true"

对于图像,分别是。然后使用

代码语言:javascript
复制
android:layout_centerHorizontal="true"

对于您的产品线和

代码语言:javascript
复制
android:layout_toLeftOf="id"  // enter id of line

代码语言:javascript
复制
android:layout_toRightOf="id" // enter id of line

要将TextView放在行的任意一侧并根据需要添加padding,请执行以下操作

票数 1
EN

Stack Overflow用户

发布于 2013-04-08 22:42:26

您需要FrameLayout,它最适合您的需求,请使用下面的布局

代码语言:javascript
复制
<?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="#ffffff"
          android:minWidth="25px"
          android:minHeight="100dp">
<TextView
        android:text="Last Game"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:gravity="center_horizontal"
        android:background="#003300" />
<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">
    <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="80dp"
            android:id="@+id/imageView1"
            android:layout_gravity="left"
            android:layout_height="80dp" />
    <TextView
            android:textColor="#000000"
            android:text="WR"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textView2"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="25dp"
            />
    <View
            android:background="#000000"
            android:layout_width="1dp"
            android:layout_height="80dp"
            android:id="@+id/view1"
            android:layout_gravity="center_horizontal" />
    <TextView
            android:textColor="#000000"
            android:text="LR"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textView3"
            android:layout_gravity="center_horizontal"
            android:layout_marginRight="25dp"
            />
    <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_gravity="right"
            android:layout_width="80dp"
            android:id="@+id/imageView1"
            android:layout_height="80dp" />
</FrameLayout>
</LinearLayout>
票数 1
EN

Stack Overflow用户

发布于 2013-04-08 22:46:09

RelativeLayout。将左侧图像设为layout_alignParentLeft=true。将中心线设置为layout_centerHorizontal=true。将WR行设置为“id of layout_align_toLeftOf=”,并将gravity=设置为“right”。在另一边重复,方向相反。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15882035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档