2016-05-29 105 views
1

我在CardView中有一个TextView和一个ImageView。
我有一个TextView的问题。
当TextView文本长于CardView长度时,它显示在ImageView上,我不希望这样。在textView旁边添加ImageView在同一行中

喜欢这幅画:

enter image description here

我想它,以便当文本触及ImageIiew,它显示...用于指示文本延续。

row.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:layout_margin="4dp"> 

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

     <TextView 
      android:id="@+id/txt_file_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true"/> 

     <ImageView 
      android:layout_width="24dp" 
      android:layout_height="24dp" 
      android:src="@drawable/ic_delete_forever_red_500_24dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true"/> 

    </RelativeLayout> 
</android.support.v7.widget.CardView> 

这是我想要的屏幕截图:

enter image description here

+1

尝试添加drawableleft(把绘制的图像),并把填充drawablepadding你的TextView –

+0

节目截图中,你想要什么。 – Ironman

+0

@Ironman我从我的目标添加屏幕截图。 –

回答

2

而不是把一个ImageView的和一个TextView在RelativeLayout的,你可以利用复合可绘制
这意味着你可以在里面添加drawables 而不是在它之外。

即:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:layout_margin="4dp"> 
     <TextView 
      android:id="@+id/txt_file_title" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_centerInParent="true" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:drawableLeft="@drawable/ic_delete_forever_red_500_24dp" 
     /> 
</android.support.v7.widget.CardView> 

这也更快,因为你马上摆脱ViewGroup中和观的(你用越少,速度更快)

另外,您还可以设置一个通过使用android:drawablePadding属性

2

请尝试下面的代码。希望它有效

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:layout_margin="4dp"> 

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

     <TextView 
      android:id="@+id/txt_file_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/imageView" 
      android:layout_marginLeft="16dp" 
      android:layout_centerVertical="true"/> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="24dp" 
      android:layout_height="24dp" 
      android:src="@drawable/ic_delete_forever_red_500_24dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true"/> 


    </RelativeLayout> 
</android.support.v7.widget.CardView> 
+0

不能正常工作! –

+0

这已经过测试。它如何工作:O。输出我的结局http://imgur.com/BcvwVFr – Masum

+0

我的意思是不适合我! –

1

使用此代码。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:layout_margin="4dp" 
    android:orientation="horizontal"> 


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

      <ImageView 
       android:layout_width="24dp" 
       android:layout_height="24dp" 
       android:src ="@drawable/ic_delete_forever_red_500_24dp" 
       android:id="@+id/imageView" 
       android:layout_centerVertical="true"/> 

      <TextView 
       android:id="@+id/txt_file_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="bjbjgbdgkgbndbndkbdkb..fg.bgbfgnbnfgb." 
       android:layout_toEndOf="@+id/imageView" 
       android:layout_toRightOf="@+id/imageView" 
       android:layout_centerVertical="true"/> 

     </RelativeLayout> 
</android.support.v7.widget.CardView> 

你的输出看起来像你想要的。

enter image description here

相关问题