【问题标题】:How to add subtext in TextView?如何在 TextView 中添加潜台词?
【发布时间】:2011-09-26 22:14:23
【问题描述】:

我想设计一个布局,其中我想要在 Android 中的主要文本正下方的子文本。 如下图所示。

 ____________________________________________
|<EMP NAME >        | Location   | ImageView |
|<designation>      |                        |
|________________________________|___________|

在 Iphone 中,UITableView 有 detaulTextLable 的选项。我怎样才能在 Android 中实现这一点。有什么想法吗?

我的 Xml 文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_below="@+id/addEmployee"
        android:layout_weight="1" android:layout_height="wrap_content"
        android:clickable="true" android:padding="2dip" android:layout_margin="5dip"
        android:layout_marginBottom="2dip" android:layout_marginTop="2dip"
        android:paddingBottom="2dip" android:background="#F5F5F5"         android:fitsSystemWindows="true" 
        android:focusableInTouchMode="true">



        <TextView android:layout_weight="1" android:id="@+id/add_employee_name"
            android:clickable="true" android:layout_width="0dp" android:text="Enter Name"
            android:layout_gravity="fill" android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip" android:layout_height="match_parent"
            android:gravity="left|center" android:textStyle="bold"
            android:paddingLeft="5dip" android:lineSpacingMultiplier="1.0"      android:lines="3">
       </TextView>

   <!--
      <TextView android:layout_weight="1" android:id="@+id/add_emp_designation"> 
      </TextView>
      Need to add a new text view for employees designation or textview has options for subtitles.
    -->


        <TextView android:layout_weight="0.4"
            android:id="@+id/add_employee_location" android:clickable="true"
            android:layout_width="0dp" android:text="Location" android:layout_gravity="fill"
            android:layout_marginLeft="0dip" android:layout_marginRight="2dip"
            android:layout_height="match_parent" android:textStyle="normal" android:paddingLeft="5dip" android:textColor="#B03060" android:gravity="bottom|right">
        </TextView>



        <ImageView android:id="@+id/employee_pic"
            android:layout_gravity="right" android:src="@drawable/edit"
            android:layout_width="wrap_content" android:layout_height="match_parent">
        </ImageView>

    </LinearLayout>

我只是对如何实现上述布局感到困惑。添加一个新的 textview 水平添加它。有什么想法吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    如果您希望 Location 和 ImageView 只占用它们所需的空间,那么以下布局就可以解决问题:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
      <LinearLayout android:layout_height="fill_parent"
        android:layout_width="wrap_content" android:id="@+id/linearLayout1"
        android:orientation="vertical" android:layout_weight="1">
        <TextView android:text="TextView" android:id="@+id/EMP_NAME"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_weight="1"></TextView>
        <TextView android:text="TextView" android:id="@+id/Description"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_weight="1"></TextView>
      </LinearLayout>
      <TextView android:id="@+id/location" android:text="TextView"
        android:layout_height="fill_parent" android:layout_width="wrap_content"
        android:gravity="center"></TextView>
      <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:id="@+id/YourImageView"
        android:src="@drawable/icon"></ImageView>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      尝试在水平线性布局中使用垂直 LinearLayout。然后用你想要的多行文本填充垂直线性布局

      【讨论】:

        【解决方案3】:

        试试这个:

        <RelativeLayout android:layout_height="50sp"
                        android:layout_width="fill_parent">
                        <TextView android:layout_alignParentTop="true"
                            android:layout_alignParentLeft="true"
                            android:id="@+id/designation"               
                            android:layout_toLeftOf="@+id/location"             
                            android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name"/>
                        <TextView android:layout_height="wrap_content"                                  
                            android:layout_alignParentBottom="true"
                            android:layout_alignParentLeft="true"
                             android:id="@+id/designation"
                            android:layout_width="wrap_content" 
                            android:layout_toLeftOf="@+id/location" android:text="Designation"/>
        
                        <TextView android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true" android:id="@+id/location"
                            android:layout_toLeftOf="@+id/image"                
                            android:gravity="center_vertical|center_horizontal" android:layout_alignParentTop="true" android:text="Location" android:layout_width="80sp"/>
        
                        <ImageView android:textColor="@color/white"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:id="@+id/image"
                            android:layout_alignParentBottom="true"             
                            android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/nologo"/>
        </RelativeLayout>
        

        【讨论】:

          【解决方案4】:

          查看相对布局,它可以让您相对于父组件定位一些组件,其余组件将相对于其他组件,您在 android 中也有带有 RowLayout 的表格布局,但我认为它不太灵活。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多