【问题标题】:position of two textview and a imageview in linearlayout线性布局中两个文本视图和一个图像视图的位置
【发布时间】:2013-07-11 05:11:47
【问题描述】:

我正在准备自定义listview。我想实现这个

我很困惑,如何将imageviewtextview 放在linearlayout 中。所以,我的问题是如何将imageviewtextview 放在linearlayout 中,如图所示。我试过了,但我仍然无法用 imageview 实现这两个 textview。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#003f84"
        android:textColor="#FFFFFF"
        android:text="TextView" />
    <View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#003f84"
        android:textColor="#FFFFFF" 
        android:gravity="right"
        android:text="TextView"/>


</LinearLayout>

不过,我无法实现这一点。但是,我要试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/abTopic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#003f84"
        android:text="TextView"
        android:textColor="#FFFFFF" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/abType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#003f84"
        android:gravity="right"
        android:text="TextView"
        android:textColor="#FFFFFF" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector"
        android:orientation="horizontal"
        android:paddingBottom="15dip"
        android:paddingRight="15dip"
        android:paddingTop="15dip" >

        <!-- Title -->

        <TextView
            android:id="@+id/abTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:contentDescription="@string/app_name"
            android:paddingLeft="25dip"
            android:paddingRight="25dip"
            android:textColor="#0099CC"
            android:textSize="18sp" />
        <!-- Rightend Arrow -->

        <ImageView
            android:id="@+id/itemArrow"
            android:layout_width="25dip"
            android:layout_height="25dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/arrow" />
        <!-- SubTitle -->

        <TextView
            android:id="@+id/absubTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="0dip"
            android:layout_toRightOf="@id/abTitle"
            android:paddingRight="25dip"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold"
            android:typeface="sans" />
    </RelativeLayout>

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-linearlayout android-imageview textview


    【解决方案1】:

    试试这个代码并根据你的需要进行更改。

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="100dp" >
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:text="TextView" />
    
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
    
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textView1"
                    android:text=" " 
                    android:background="#000000"/>
    
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView1"
                    android:layout_below="@+id/textView1"
                    android:layout_marginTop="25dp"
                    android:text="TextView" />
    
                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/textView3"
                    android:layout_below="@+id/textView3"
                    android:layout_marginTop="14dp"
                    android:text="TextView" />
    
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignTop="@+id/textView3"
                    android:layout_marginRight="47dp"
                    android:src="@drawable/ic_launcher" />
    
                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textView2"
                    android:layout_marginRight="14dp"
                    android:text="TextView" />
    
            </RelativeLayout>
    
        </FrameLayout>
    

    【讨论】:

      【解决方案2】:

      LinearLayouts 具有android:gravity 属性,可以将其子元素尽可能移到一个边框。在您的情况下,您需要一个具有两个 LinearLayout 子级的每一行的 LinearLayout。在第一个 LinearLayout 中执行 android:gravity="left",在第二个中执行 android:gravity="right"

      【讨论】:

        【解决方案3】:

        你为什么不把你的图片放到 textview 的背景中呢?您可以将android:gravity="right" 用于您的某些列表项

        【讨论】:

          【解决方案4】:

          请使用此代码并根据您的需要进行调整:

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >
          
              <TextView
                  android:id="@+id/textView1"
                  android:layout_width="90dp"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="30dp"
                  android:background="#003f84"
                  android:text="Text1"
                  android:textColor="#FFFFFF" />
          
              <TextView
                  android:id="@+id/textView2"
                  android:layout_width="90dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="120dp"
                  android:layout_marginTop="-18dp"
                  android:background="#003f84"
                  android:text="Text2"
                  android:textColor="#FFFFFF" />
          
              <ImageView
                  android:id="@+id/Image1"
                  android:layout_width="fill_parent"
                  android:layout_height="90dp"
                  android:layout_gravity="center"
                  android:layout_marginTop="50dp"
                  android:src="@drawable/ic_launcher" />
          
          </LinearLayout>
          

          如果有任何疑问,请随时给我写信。谢谢

          【讨论】:

            【解决方案5】:

            我可以自己解决我的问题。

              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
            
                    <!-- Topic -->
            
                    <TextView
                        android:id="@+id/abTopic"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#003f84"
                        android:textColor="#FFFFFF" />
            
                    <!-- View -->
            
                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:background="@android:color/darker_gray" />
            
                    <!-- Type -->
            
                    <TextView
                        android:id="@+id/abType"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#003f84"
                        android:gravity="right"
                        android:textColor="#FFFFFF" />
            
                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/list_selector"
                        android:orientation="horizontal"
                        android:paddingBottom="15dip"
                        android:paddingRight="15dip"
                        android:paddingTop="15dip" >
            
                        <!-- Title -->
            
                        <TextView
                            android:id="@+id/abTitle"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:contentDescription="@string/app_name"
                            android:paddingLeft="25dip"
                            android:paddingRight="25dip"
                            android:textColor="#000000"
                            android:textSize="18sp" />
            
                        <!-- Rightend Arrow -->
            
                        <ImageView
                            android:id="@+id/itemArrow"
                            android:layout_width="25dip"
                            android:layout_height="25dip"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:contentDescription="@string/app_name"
                            android:src="@drawable/arrow" />
            
                        <!-- Subtitle -->
            
                        <TextView
                            android:id="@+id/SubTitle"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/abTitle"
                            android:paddingLeft="25dip"
                            android:paddingRight="25dip"
                            android:textColor="#666362"
                            android:textSize="12sp"
                            android:textStyle="bold"
                            android:typeface="sans" />
                    </RelativeLayout>
            
                </LinearLayout>
            

            【讨论】: