1

解決策がテキストビューにlayout_weight = "1"を追加している類似の質問がたくさんあります。私はそれを試みたが、それは私を助けなかった。私のシナリオは異なります。それが重複しているとは考えないでください。LinearLayoutのTextViewがラップしてImageViewに移動しない

以下は私が作りたいレイアウトです。イメージビューにイメージがある場合、TextViewはラップする必要があります。画像がない場合はそのままでなければなりません。以下は

enter image description here

私がこれまでに以下

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
app:cardBackgroundColor="@android:color/white" 
app:cardCornerRadius="2dp"> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     android:layout_alignParentStart="true" 
     android:gravity="start|center_vertical" 
     android:orientation="vertical"> 
     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="some text here"/> 
     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="some text here which should wrap depending upon 
      the string length and also considering whether or not there is an image adjacent to it"/> 
     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="some text here"/> 
    </LinearLayout> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="10dp" 
     android:adjustViewBounds="true" 
     android:src="@drawable/save"/> 
</RelativeLayout> 
</android.support.v7.widget.CardView> 

を試したコードは、このコードの結果です。 TextViewはラッピングなしでImageViewの下にあります。

enter image description here

間違って私のアプローチです。誰かが私にこれを解決する手助けをすることができます私は、mini APIレベル21で開発しているAndroidStudio 3.0を使用しています(これはすべて重要です)。 ありがとうございます

答えて

1

をしたいとTextViewには、それが動作する画像がない場合。相対的なレイアウトは必要ありません。この相対レイアウトをlinearlayoutで水平に変更します。その後、垂直のLinearLayout幅0dpを変更し、重量を追加:1

XMLコード:迅速な対応のための

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    app:cardBackgroundColor="@android:color/white" 
    app:cardCornerRadius="2dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="some text here" 
       android:layout_weight="1"/> 
      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="some text here which should wrap depending upon 
      the string length and also considering whether or not there is an image adjacent to it"/> 
      <TextView 
       android:id="@+id/textView3" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:text="some text here"/> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginStart="10dp" 
      android:layout_marginEnd="10dp" 
      android:adjustViewBounds="true" 
      android:src="@mipmap/ic_launcher"/> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 
+0

感謝。それは働いている。しかし、あなたはこのことを説明できますか?なぜこのようなことが起き、layout_weight – Krishna

+0

の水平線形レイアウトの魔法は、2つの子供と1つの垂直線形レイアウトと別の画像を持っています。垂直線形レイアウトをweight:1に設定します。これは、imageviewにウェイトがないため、常に100%のスペースを取ることを意味します。画像が利用可能なときは、画像空間を除く100%の空間が必要となり、画像が利用できないときは画像なしで親の100%の空間が必要になります。 – Yeahia2508

1

RelativeLayoutをLinearLayoutに変更し、向きを水平にします。 それでも動作しない場合は、ImageViewにウェイトを追加してください。それは動作するはずです。

1

私はあなたが相対レイアウトを必要としないと思います。

この線形レイアウトを5回使用する必要があります。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     android:orientation="horizontal"> 
     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="some text here which should wrap depending upon 
     the string length and also considering whether or not there is an image adjacent to it" 
      android:layout_weight="1" 
      /> 
     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:adjustViewBounds="true" 
      android:src="@drawable/ic_launcher_background" 
      /> 
    </LinearLayout> 

あなたはこれが正確に何が必要に動作します enter image description here

関連する問題