开发者

How to add subtext in TextView?

I want to design a Layout in which I want subtext right below main text in Android. Pictorially something like below.

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

In Iphone, UITableView there is option for detaulTextLable. How can I achieve this in Android. Any Idea?

My Xml file goes like below:

    <?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_design开发者_StackOverflow社区ation"> 
      </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>

I am just confused how to achieve above layout. Adding a new textview adds it horizontally. Any idea?


Provided that you want the Location and ImageView to take up only just as much space as they need, the following layout will do the trick:

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


Try using a vertical LinearLayout inside your horizontal linear layout. and in turn fill vertical LinearLayout with your desired multi line text


try this one:

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


check out Relative Layout it will let you position some components relative to the parent and the rest will be relative to the other components you also have Table Layout with RowLayout in android but it's less flexible in my opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜