2012-01-14 8 views
0

私は、ダイアログのレイアウトをプログラムで変更したいと思います。ルートLinearLayoutの向きを変更します。このルートLinearLayoutには、GTとDIという2つのレイアウトが含まれています。 AlertDialogを使用すると効果的ですが、Dialogを直接使用する場合(タイトルやタイトルスペースを必要としないためカスタムスタイルのダイアログを使用しています)は役に立ちません。ここで Android:ダイナミックダイアログのレイアウト変更

は私のXMLレイアウトです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/root_container" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<LinearLayout 
    android:id="@+id/GT_container" 
    android:layout_width="wrap_content" 
    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="GT" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <RadioGroup 
     android:id="@+id/GT_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radioButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:height="5dp" 
      android:text="GT #1" /> 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:height="5dp" 
      android:text="GT #2" /> 

    </RadioGroup> 
</LinearLayout> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/DI_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#FF0000" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="DI" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scrollbarAlwaysDrawVerticalTrack="true" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

      <RadioGroup 
       android:id="@+id/DI_group" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

       <RadioButton 
        android:id="@+id/DI_rdb_1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:checked="true" 
        android:text="DI #1" /> 

       <RadioButton 
        android:id="@+id/DI_rdb_2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:checked="true" 
        android:text="DI #2" /> 
      </RadioGroup> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

そして、ここに私のコードです:私はXMLの重複を避けるために、 "レイアウトの土地" を使用したくない

LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View rootLayout = inflater.inflate(R.layout.new_game, (ViewGroup) activity.findViewById(R.id.root_container)); 

if (screenIsLandscape) { 
    ((LinearLayout) rootLayout).setOrientation(LinearLayout.HORIZONTAL); 
} else { 
    ((LinearLayout) rootLayout).setOrientation(LinearLayout.VERTICAL); 
} 

Dialog dlg = new Dialog(activity, R.style.FullHeightDialog); 
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dlg.setContentView(R.layout.my_layout); 
dlg.show(); 

(私のXMLむしろ大きくなっていますが、ここでは単純化しています)。 ありがとうございます。

+0

あなたは2を切り替えるために)(setContentViewを変更しようとしましたか? –

+0

ありがとうございます。申し訳ありませんが、私は理解していません:何と何を切り替えるのですか? setContentView()では、私は現在、私の唯一のXMLリソースレイアウトファイルの名前である 'R.layout.my_layout'を使用しています。あなたはGTとDIの間の切り替えを意味する場合、それは私が探しているものではありません:私は水平から垂直に、またはその逆にレイアウトしたいと思います – xav

答えて

3

xml-fileから新しいオブジェクト "rootLayout"を作成し、それを変更しました(rootLayout、xml)。このオブジェクトはあなたのファイルにリンクされていません。

あなたが変更されたオブジェクトを持って、それを使用する:

dlg.setContentView(rootLayout);

+0

あなたが正しいと思いますレコード – xav

関連する問題