2017-01-19 71 views
0

我有一個ImageView對齊到TextView的右邊。當文本太長時,它會調整圖像大小...如何保持圖像的大小相同並將TextView強制爲新行?不能修復ImageView裏面的RelativeLayout

短文本,良好的大小:
enter image description here

長文本,壞的大小:
enter image description here

這裏是XML:

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

    <RelativeLayout 
     android:id="@+id/btn_container_1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="@color/blue" 
     android:layout_weight=".50" 
     android:layout_marginRight="1dp"> 

     <Button 
      android:id="@+id/btn_1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:alpha="0" 
      android:onClick="searchClicked"/> 

     <TextView 
      android:id="@+id/btn_text_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:textColor="@color/md_white_1000" 
      android:paddingEnd="10dp" 
      android:paddingRight="10dp" 
      android:text="BTN1" /> 

     <ImageView 
      android:id="@+id/btn_image_1" 
      android:layout_width="28dp" 
      android:layout_height="28dp" 
      android:layout_centerVertical="true" 
      android:layout_toEndOf="@id/btn_text_1" 
      android:layout_toRightOf="@id/btn_text_1" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp" 
      android:src="@drawable/draw" /> 
    </RelativeLayout> 
</LinearLayout> 
+0

在Textview標籤中嘗試android:ellipsize =「end」 – Onkar

回答

1

您可以添加一個嵌套的線性佈局這樣做類似如下:

<RelativeLayout 
    android:id="@+id/btn_container_1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:background="@color/blue" 
    android:layout_weight=".50" 
    android:layout_marginRight="1dp"> 

    <Button 
     android:id="@+id/btn_1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:alpha="0" 
     android:onClick="searchClicked"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     > 

     <TextView 
      android:id="@+id/btn_text_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:textColor="@color/md_white_1000" 
      android:paddingEnd="10dp" 
      android:paddingRight="10dp" 
      android:text="BTN1" /> 

     <ImageView 
      android:id="@+id/btn_image_1" 
      android:layout_width="28dp" 
      android:layout_height="28dp" 
      android:layout_gravity="center" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp" 
      android:src="@drawable/draw" /> 

    </LinearLayout> 
</RelativeLayout> 
1

我認爲你應該使用線性佈局水平方向而不是相對佈局,併爲textview和imageview設置權重。

0

不要硬編碼任何屬性將在稍後創建問題,如果屏幕尺寸的變化試試這個

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:weightSum="1" 
    android:layout_marginTop="100dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

     <Button 
      android:id="@+id/btn_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:alpha="0" 
      android:text="button" 
      android:layout_weight="0.2" 
      android:onClick="searchClicked"/> 

     <TextView 
      android:id="@+id/btn_text_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:textColor="@color/colorPrimary" 
      android:layout_weight="0.4" 
      android:text="BTN1" /> 

     <ImageView 
      android:id="@+id/btn_image_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.4" 
      android:src="@drawable/state" /> 
</LinearLayout> 
0
Can you try this, if it is not working send me you screen how you want to design.I will help you. 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="est.androidapplication.MainActivity"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:background="#00f" 
     android:weightSum="1"> 
     <TextView 
      android:id="@+id/btn_text_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:textColor="#ff0044" 
      android:paddingEnd="10dp" 
      android:paddingRight="10dp" 
      android:text="BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1BTN1" /> 

     <ImageView 
      android:id="@+id/btn_image_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:src="@mipmap/ic_launcher" /> 
    </LinearLayout> 
</RelativeLayout> 
0

在線性佈局中使用textview的權重。

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_margin="10dp" 
    android:layout_centerVertical="true"> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="BTN1"/> 
    <ImageView 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_marginLeft="10dp" 
     android:background="#344"/> 

</LinearLayout> 


</RelativeLayout> 
相關問題