2012-04-30 9 views
0

Androidのレイアウトに固定幅を占有:は、私は私のレイアウトの一部としてこれを持って

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:layout_weight="0.15"> 

     <TextView 
      android:id="@+id/question_text" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:text="@string/score_label" /> 

     <TextView 
      android:id="@+id/score_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" /> 

</LinearLayout> 

最初TextViewアプリケーションの開始時に空です。その内容は動的に変更されます。これにより、0のスペースが占有され、layout_gravityrightに設定されていても、2番目のTextViewが左に揃えられます。

内容を考慮せずに、固定幅を占めるようにするにはどうすればよいですか?
私はlayout_weightを使用することを考えましたが、ネストされた重み(親ViewGroupにはlayout_weight属性があります)を使用することをお勧めします。たぶんRelativeLayoutを使用する必要がありますか?

ありがとうございます。

+0

あなたの投稿にレイアウトの画像を追加することができます(スクリーンショットやスケッチで十分です)。これは他の人がレイアウトをはるかに速く理解するのに役立ちます。 – JJD

答えて

1

TextViewsの幅はすべてandroid:layout_width="wrap_content"に設定されています。つまり、何もない場合は幅がありません。その値を"match_parent"に設定すると、親コンテナと同じ幅になるか、固定値に設定されます(android:layout_width="100dp"など)。

+0

実際、最初のTextViewの幅は0dipです。これはfill/match_parentと同じです。 – user940016

+0

layout_weightも1に設定した場合は、fill_parent/match_parentと同じです。 – CaseyB

3

TextViewの属性android:ems="<some number>"を使用して同様の問題を解決しました。 「ems」は、文字「M」の幅である。この属性はTextViewを指定されたnoと正確に一致させます。のemsワイドの。 http://developer.android.com/reference/android/widget/TextView.html

+0

確認しますが、ほかの誰かが別のアイデアを持っている場合は、返信することができます。 – user940016

関連する問題