2016-05-29 104 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

相關問題