2011-10-28 10 views
5

私は新しいxmlレイアウトを作成して表示するためにメインレイアウトを拡張できるように変更した簡単なSwipeSampleを見つけました。私がやりたかったことは、プログラムでスワイププロセスのレイアウトを追加できることでした。プログラムでビューを膨らませる最善の方法

Iはmain.xmlレイアウトと単色に設定のTextViewと簡単のLinearLayoutありred.xmlとyellow.xmlを有します。

作品次のコードが、私はそれは私が取得しようとしているものを行うための最善の方法が正しいのですかとは思いません。 誰かが大いに感謝するより良い方法を提案できる場合。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Create a layout with a solid blue background programmatically 
    TextView tv1 = new TextView(this); 
    tv1.setText("Blue"); 
    tv1.setBackgroundColor(Color.BLUE); 
    tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll.addView(tv1); 
    //Create a layout with a solid green background programmatically 
    TextView tv2 = new TextView(this); 
    tv2.setText("Green"); 
    tv2.setBackgroundColor(Color.GREEN); 
    tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    LinearLayout ll2 = new LinearLayout(this); 
    ll2.setOrientation(LinearLayout.VERTICAL); 
    ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll2.addView(tv2); 
    //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts 
    fSpace = (ViewFlipper)findViewById(R.id.flipper); 
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.yellow, fSpace); 
    inflater.inflate(R.layout.red, fSpace); 
    fSpace.addView(ll); 
    fSpace.addView(ll2); 

} 

答えて

2

あなたがR.layout.yellowとR.layout.redを膨張させる方法は本当にそうする正しい方法です。たくさんのコードをxmlに移動することで、コードを単純化することができます。私はtv1は単なるサンプルだと思いますか?そうでなければ、main.xmlに入ることができます。あなたは、あなたがやっていることに応じて、単一のインフレーションで黄色と赤を作成する方法を見つけることさえできます。

はプログラムでビューを作成するだけで、ほとんどの部分は、やや面倒です。

4

あなたがプログラムで作成する複雑なレイアウトを持っている場合、それはXMLでレイアウト既成のを持って、その後、それを膨らませると、実行時にそれを追加するのが最も簡単かもしれません。

ここ

レイアウトフォルダにあるサンプルあらかじめ作られたXMLレイアウトがあるXML

でビューを作成します。あなたは何でも、単一のビュー、または複雑なレイアウト全体であってもかまいません。

レイアウト/ my_view.xml

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/TextView1" 
     android:text="This is a TV"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/TextView2" 
     android:text="How are you today?"/> 
</LinearLayout> 

ビュー

のコンテナは、それがあなたの活動のレイアウトでビューを置くいくつかの場所があることを確認してください。あなたはこのようなことをすることができます。

<FrameLayout 
    android:id="@+id/flContainer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

</FrameLayout> 

使用して、コンテナへの参照を取得し、XMLからビューを膨らませる、その後、コンテナに追加するビューを膨らませます。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FrameLayout container = (FrameLayout) findViewById(R.id.flContainer); 
    View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false); 
    container.addView(inflatedLayout); 

} 

このようにすると、コードがきれいになります。

も参照してください:

関連する問題