2016-04-05 3 views
1

私は次の単純なレイアウトになっています。RelativeLayout 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"> 

<TextView 
    android:id="@+id/x" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginEnd="@dimen/margin_small" 
    android:layout_marginRight="@dimen/margin_small" 
    android:text="@string/long_string" 
    android:textAppearance="?android:attr/textAppearanceSmall"/> 
<CheckBox 
    android:id="@+id/y" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/x" 
    android:layout_toEndOf="@+id/x" 
    android:layout_centerVertical="true" /> 

</RelativeLayout> 

このレイアウトは、テキストビューのテキストの長さが短い場合に問題ありません。チェックボックスは、テキストビューの右側に配置されます。しかし、テキストが長くなり、たぶんラップする場合でも、チェックボックスはビューから押し出されます。それはもはや見えません。私はチェックボックスが常に画面の幅全体を埋める場合でも、テキストビューの右側に表示されるようにしたいと思います。

レイアウトをLinearLayoutで書き直そうとしましたが、どちらも動作しません。

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

<TextView 
    android:id="@+id/x" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="@dimen/margin_small" 
    android:layout_marginRight="@dimen/margin_small" 
    android:text="@string/long_string" 
    android:textAppearance="?android:attr/textAppearanceSmall"/> 
<CheckBox 
    android:id="@+id/y" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout> 

相対レイアウトでこれを達成するための秘訣はありますか?私はどういうわけか、デフォルトで相対レイアウトからこの動作を期待しています。おかげで、あなたはあなたのビュー(のTextViewとチェックボックス)を作るために、プロパティlayout_weightを置くべき

答えて

1

のlayout_weightされるようにこれは私のために働いているチェックボックス:alignParentRightを作り、それtoLeftOfの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"> 

    <TextView 
     android:id="@+id/x" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/y" 
     android:text="This is very-very-very looooooooooooong stringgggg, very-very, long-long" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 
    <CheckBox 
     android:id="@+id/y" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true"/> 

</RelativeLayout> 

編集。また、働いて

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:gravity="left"> 

     <TextView 
      android:id="@+id/x" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toLeftOf="@+id/y" 
      android:text="this is veryyyyy yyyyyyyyyy yyyyyy yyy loooooo oooooooo ooon nnggggg gggg striiii iiiiin gggggg ggggg ggggggg ggg" 
      android:textAppearance="?android:attr/textAppearanceSmall"/> 
     <CheckBox 
      android:id="@+id/y" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:layout_centerVertical="true" />  

    </RelativeLayout> 

</RelativeLayout> 

あなたは他の(親の)レイアウトにこの相対的なレイアウトを含めることができます。 Relativeレイアウトにandroid:gravity = "left"を置くと、そのコンテンツが左側に配置されます。

+0

私はその可能性を認識していますが、このレイアウトが大きなスクリーンのタブレットではうまく動作しないため、チェックボックスを親の右側ではなくテキストビューの右側にしたいと尋ねました。短い文字列では、2つの要素があまりにも離れているためです。 –

+0

あなたはこの相対レイアウトを別のアイテム(ビュー)として取り、それをあなたの親レイアウトに含めることができます。 –

+0

あなたは何を意味するのか分かりません。明確にできますか?このレイアウトを親ビューに含めても、何も振る舞いは変わりません:) –

1

)が出すことであろう代わりに使用ハード値

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/x" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.9" 
     android:layout_marginEnd="@dimen/margin_small" 
     android:layout_marginRight="@dimen/margin_small" 
     android:text="@string/long_string" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 
    <CheckBox 
     android:id="@+id/y" 
     android:layout_weight="0.1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    </LinearLayout> 
+0

ありがとうございますが、これは基本的に私が親の右側にチェックボックスを配置し、テキストボックスの左側に配置するのと同じ効果があります。結果は携帯電話やタブレットでは満足できません。私はテキストが短いです、そして、チェックボックスはほぼ親ですが、テキストビューの右側にはありません。 –

1

一つの方法の画面でdeffはスペースを持っていますtextviewとチェックボックスを水平方向の線形レイアウトにします。セットあなたが好きなことするチェックボックスの幅(定数)とテキストボックスの幅が0dpと1

+0

ありがとうございますが、これは基本的に他の2つの回答と同じです。私はそのレイアウトをしたい場合、私は相対レイアウトをチェックボックスの親の権利を整列させるだろう。 –

0

デフォルトでは、希望のレイアウトはできないと思います。 RelativeLayout、LinearLayout、TableLayoutを使ってこれをしようとしました。これらのレイアウトはその動作をサポートしていないことは技術的に理解できます。相対レイアウトは、左または右に配置されていても、左または右の要素が親内部で最小限に見える場合を明示的に尊重しなければならない。もう1つの解決策は、表レイアウトによって列が残りの領域を消費することができ、他の列の最小幅も尊重する場合です。

私の場合、私は回避策を書いた。私は私の質問の最初の相対的なレイアウトを使用しますが、次の計算を使用してのTextViewに最大幅を設定します。

DisplayMetrics displaymetrics = new DisplayMetrics(); 
    context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int displayWidth = displaymetrics.widthPixels; 

見えるようにする]チェックボックスを保証します。私は、与えられたレイアウトがより複雑なレイアウトに埋め込まれているシナリオでは、ほとんど解決できない可能性があることを知っています。

関連する問題