2011-09-26 75 views
0

我想要在Android中的主文本下方設計一個我希望使用子文本的佈局。 像以下一樣。如何在TextView中添加子文本?

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

我只是困惑如何實現上述佈局。添加新的文本視圖會橫向添加它。任何想法?

回答

1

前提是你想要的位置和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> 
1

嘗試在水平線性佈局內使用垂直LinearLayout。並依次填入垂直的LinearLayout與你期望的多行文字

1

試試這個:

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

退房相對佈局它會讓你定位一些相對於父,其餘組件將相對於其他組件在android中也有RowLayout的表格佈局,但我認爲它不太靈活。