2014-10-20 47 views
0

让我们来看看下面的布局:裹的TextView

example

现在让我们做一些限制:

  • 的TextView必须对准左
  • 图片必须与左边的textView文本对齐
  • CheckBox必须与右边对齐
  • TextView的可能是2行以上(可能是一个很长的文本或短的一个)

所以我想它的运行方式是,只要文本是足够短的剩余空间会图像和复选框之间。

但是,如果文字足够长,我只需要textView分成两行,即 ,因为它足够长,图像将被推入,直到它也与复选框对齐。 (但它仍然对齐textView ...)

如果我使用LinearLayout我必须制作宽度为0和重量为1的textView,以便在没有更多空间时进行拆分,但是在这种情况下图像将不会与textView的文字对齐....

所以我需要在图像和复选框之间加上一些间隔以及重量,但是它会破坏textView的重量。

一个相对布局不会让TextView的知道什么时候是时候拆....

为TextView的硬编码的宽度将不发挥好为好,因为图像不会被对齐文本,而不同的设备有不同的宽度。

带有drawableRight的textView不起作用。

任何想法我怎么能实现这种行为?

+0

你必须使用重量和线性布局,以实现它并设置宽度和重量根据您的需要也对齐元素。它会正常工作.. – Umair 2014-10-20 12:47:06

回答

1

试试这种方式,希望这会帮助你解决你的问题。

设置你形象的TextView的右抽拉

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/checkbox" 
     android:gravity="left"> 
     <TextView 
      android:id="@+id/textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/ic_launcher" // here set you image 
      android:text="android demo text"/> 

    </LinearLayout> 
</RelativeLayout> 
+0

不错! ,你可以改变linearLayout为frameLayout。 Tnx! – ndori 2014-10-20 13:41:08

+0

很高兴帮助你... – 2014-10-20 14:57:33

0

尝试这样可以帮助你,

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

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="85" 
      android:gravity="center" 
      android:orientation="horizontal" > 

       <TextView 
      android:id="@+id/textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/ic_launcher" 
      android:text="android demo text"/> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="15" 
      android:gravity="center|right" 
      android:orientation="vertical" > 

      <CheckBox 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

    </LinearLayout> 
+0

在这个解决方案中,尺寸不是动态的,因为复选框总是会占用一定的空间(因为15个重量),所以包裹的文本只能增长到85个重量,并且可能会有很大的空间左.. 认为一个巨大的平板电脑,并有很多文字,线路将中断,仍然会有很多空间留下线。另一方面,如果checkBox在一个小设备上足够大,它可能会比线的15重量大,所以它不会正确显示 – ndori 2014-10-20 13:05:48