2013-07-09 23 views
0

我想实现下面的布局,其中有一个textview和图像的修复空间。如何正确使用重量属性?

我使用相对布局,它没有“重量”属性。如何使用LinearLayout实现以下布局?

enter image description here

下面是我的代码

   <RelativeLayout> 
       <TextView 
      android:id="@+id/txt" 
      android:paddingLeft="5dp"    
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"     
      android:gravity="left" 
      android:textSize="14dp"  
      android:textColor="@android:color/white"/>   

       <TextView 
        android:id="@+id/date_n_time" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
      android:padding="5dp"   
        android:textColor="#E0FFFF"  
        android:layout_below="@+id/txt" /> 

       <ImageView android:id="@+id/imageview" 
        android:layout_height="wrap_content" 
        android:paddingRight="5dp"   
        android:contentDescription="Image" 
        android:layout_width="wrap_content"   
        android:layout_alignParentRight="true" 
        android:src="@drawable/icon"/> 
     </RelativeLayout> 

我已经使用的RelativeLayout用于放置的TextView和图像像上述图像。但是textview和imageview是相互重叠的。

+1

固定的空间,可以简单地保证金 – njzk2

回答

0

您可以像这样轻松地将TextView放置在图像的左侧:

android:layout_toLeftOf="@+id/yourimg" 

添加保证金,你应该能够很容易地实现这一布局

+0

@ benjamin:谢谢,我的问题解决了。 – user1740281

0

您可以通过在文本视图右侧或图像视图左侧提供填充/边距来提供固定空间。或者,如果您想使用线性布局,则在文本视图中提供7的权重,在图像视图中提供3的权重。

0

使用LinearLayoutlayout_weightSumlayout_weight

下面是将屏幕垂直分成两部分的示例。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weigthSum="1"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.9"> 

     <ListView 
      android:id="@+id/frontpagetasklistid" 
      android:layout_width="fill_parent" 
      android:layout_height="375dp" > 

     </ListView> 
     </LinearLayout> 
     <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1"> 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      > 
     <EditText 
      android:id="@+id/edittextidAddTodayTask" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:background="@drawable/edittextselector" 
      android:hint="Add Today Task"/> 

     <ImageButton 
      android:id="@+id/imagebuttonidAddTodayTask" 
      android:layout_width="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:src="@drawable/addtask" /> 

     </RelativeLayout> 
     </LinearLayout> 

    </LinearLayout> 
+0

@ Brijesh塔库尔:谢谢你的帮助。 – user1740281

0

你应该使用的LinearLayout与水平方向,里面放一个TextView和ImageView的,同时设置layout_width到0dp和体重,你希望他们有什么相对权重,例如

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

<TextView 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 
/> 

<ImageView 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 

/> 
</LinearLayout> 

textView的权重为4,imageView的权重为1,这意味着textview会获得4/5的空间,而textView将获得1/5的空间,试试吧

+0

谢谢Dan Levin回复。 – user1740281