2016-05-10 7 views
-1

これらの要素のそれぞれまたはすべてをプログラマチックに作成する方法はわかっていますが、これらのLinearLayoutsをウェイトなどで配置する方法がわからないのですが、次のXMLをプログラマチックに生成するのに役立ちますか?アクティビティからプログラムでこのXMLを作成するにはどうすればよいですか?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/background_main" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:id="@+id/main" 
      tools:context=".MainActivity" 
      android:weightSum="3"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:id="@+id/sa_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:background="@drawable/sa_id_small" 
      android:onClick="onSAIDClicked" 
      android:visibility="invisible" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 
    </RelativeLayout> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <EditText 
      android:id="@+id/phone_number" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:inputType="phone" 
      android:singleLine="true" 
      android:textColor="#FFF" 
      android:textColorHint="#FFF"/> 

     <EditText 
      android:id="@+id/pin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/phone_number" 
      android:layout_centerHorizontal="true" 
      android:hint="Pin Code" 
      android:imeOptions="actionDone" 
      android:inputType="numberPassword" 
      android:maxLength="4" 
      android:singleLine="true" 
      android:textColor="#FFF" 
      android:textColorHint="#FFF"/> 

     <Button 
      android:id="@+id/login" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/pin" 
      android:layout_centerHorizontal="true" 
      android:onClick="onButtonClicked" 
      android:text="Register"/> 

     <Button 
      android:id="@+id/usa_drivers" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:background="@drawable/usa_drivers_dc_small" 
      android:onClick="onUSADriversClicked" 
      android:visibility="invisible" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 
    </RelativeLayout> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:id="@+id/usa_passport" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:background="@drawable/usa_passport_small" 
      android:onClick="onUSAPassportClicked" 
      android:visibility="invisible" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 
    </RelativeLayout> 

</LinearLayout> 

おかげで、 Wihan

+0

なぜあなたはそれをプログラム的にしたいですか? – pskink

+0

私はこれらの要素をデータベースから動的に引き出す必要があるので、なぜそれが重要なのですか? –

+0

描画?あなたは何を描きたいのですか?何を正確に達成したいですか? – pskink

答えて

0

あなたはこれを行うことができます。例えばのため:

 // Create a new RelativeLayout 
     RelativeLayout relativeLayout = new RelativeLayout(this); 

     // Defining the RelativeLayout layout parameters. 
     // In this case I want to fill its parent 
     RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.FILL_PARENT, 
       RelativeLayout.LayoutParams.FILL_PARENT); 

     setContentView(relativeLayout, layoutParams); 
2

次の例で使用することができますプログラムでレイアウト体重、および他のレイアウト設定を設定するには

LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT); 
childParam1.weight = 0.3f; 
child1.setLayoutParams(childParam1); 

LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT); 
childParam1.weight = 0.7f; 
child2.setLayoutParams(childParam2); 

parent.setWeightSum(1f); 
parent.addView(child1); 
parent.addView(child2); 
0

を、LinearLayoutに、LinearLayout.LayoutParamsのインスタンスにプロパティを設定します。レイアウトの重みは、コンストラクタの3番目の引数です。

LinearLayout layout = new LinearLayout(context); 
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.FILL_PARENT, 1.0f); 
layout.setLayoutParams(param); 

LinearLayout API DocsはあなたのXMLからデータを符号化するためのさまざまなプロパティやセッターに詳細を提供します。

一般に、何をしようとしているかに応じて、XMLでレイアウトを定義し、関連する要素IDを与え、findViewById(R.id.yourElementId)を使用してプログラムでアクセスする方がよい場合があります。

どのメソッドを使用するのか、XMLとJava APIの間を移動するのか疑問に思っている人は、ドキュメントを読むのが一番良い方法です。 ButtonRelativeLayoutのドキュメントもご覧ください。あなたはこのように簡単にこれを行うことができます

0

:例Switchを次のように

TextViewRelative Viewがprogramaticaly活動から作成され、あなたはsuper.onCreate(savedInstanceState)後にこのコードを使用して実行するだけで setContentView(R.layout.activity_main) を使用する必要はありません。

//UI Views 
Switch btn_toggle_location, btn_toggle_state; 
TextView txt_log; 
RelativeLayout ad_container_top; 
RelativeLayout ad_container_bottom; 
RelativeLayout main_layout; 

//Define properties of Switch 1 
btn_toggle_location = new Switch(this); 
btn_toggle_location.setChecked(true); 
btn_toggle_location.setClickable(true); 
btn_toggle_location.setId(1); 

//Define properties of Switch 2 
btn_toggle_state = new Switch(this); 
btn_toggle_state.setChecked(true); 
btn_toggle_state.setClickable(true); 
btn_toggle_state.setId(2); 

//Define properties of TextView 
txt_log = new TextView(this); 
txt_log.setMovementMethod(new ScrollingMovementMethod()); 
txt_log.setId(3); 

//Define Layout parameters 
RelativeLayout.LayoutParams params_btn_location = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT); 
params_btn_location.addRule(RelativeLayout.CENTER_IN_PARENT); 
params_btn_location.setMargins(10, 0, 10, 0); 
btn_toggle_location.setLayoutParams(params_btn_location); 

RelativeLayout.LayoutParams params_btn_state = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT); 
params_btn_state.setMargins(10, 10, 10, 0); 
params_btn_state.addRule(RelativeLayout.BELOW, btn_toggle_location.getId()); 
btn_toggle_state.setLayoutParams(params_btn_state); 

RelativeLayout.LayoutParams params_log = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT); 
params_log.addRule(RelativeLayout.BELOW, btn_toggle_state.getId()); 
params_log.setMargins(10, 0, 10, 0); 
txt_log.setLayoutParams(params_log); 

//Add switch click listeners 
btn_toggle_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     ad_container_bottom.removeAllViews(); 
     ad_container_top.removeAllViews(); 
     txt_log.setText(""); 
     if (isChecked) { 


     } else { 

     } 
    } 
}); 

btn_toggle_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     txt_log.setText(""); 
     if (isChecked) { 

     } else { 

     } 
    } 
}); 

main_layout = new RelativeLayout(this); 
RelativeLayout.LayoutParams main_lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.MATCH_PARENT); 

ad_container_top = new RelativeLayout(this); 
RelativeLayout.LayoutParams layout_top = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT); 
layout_top.addRule(RelativeLayout.CENTER_VERTICAL); 
layout_top.addRule(RelativeLayout.CENTER_HORIZONTAL); 
layout_top.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
ad_container_top.setLayoutParams(layout_top); 

ad_container_bottom = new RelativeLayout(this); 
RelativeLayout.LayoutParams layout_bottom = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT); 
layout_bottom.addRule(RelativeLayout.CENTER_VERTICAL); 
layout_bottom.addRule(RelativeLayout.CENTER_HORIZONTAL); 
layout_bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
ad_container_bottom.setLayoutParams(layout_bottom); 

//Set view visibility 
ad_container_bottom.setVisibility(View.VISIBLE); 
ad_container_top.setVisibility(View.VISIBLE); 


//Finally add all views to main layout here 
main_layout.addView(btn_toggle_location, params_btn_location); 
main_layout.addView(btn_toggle_state, params_btn_state); 
main_layout.addView(txt_log, params_log); 
main_layout.addView(ad_container_top, layout_top); 
main_layout.addView(ad_container_bottom, layout_bottom); 
main_layout.setBackgroundColor(Color.WHITE); 
this.addContentView(main_layout, main_lp); 
関連する問題