2016-09-23 4 views
-1

でプログラムレイアウトを積み上げ設定Iは、それらの間でのネストされた線形レイアウトでの相対的なレイアウトを持っていると思いますが、私はこれは私が私のxmlファイルとして持っているものがどのように病気の巣にそれらをプログラムはアンドロイド

理解することができません。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<Button 
    android:id="@+id/button_top" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_margin="5dp" 
    android:text="Add items" /> 

<LinearLayout 
    android:id="@+id/above_part" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button_top" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2" 
     android:orientation="vertical" 
     android:padding="2dp"> 

     <TextView 
      android:id="@+id/label_farmerprovince" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Province" 
      android:textSize="12sp" 
      android:textStyle="normal" /> 

     <EditText 
      android:id="@+id/fivms_farmerprovince" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background"> 

     </EditText> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2" 
     android:orientation="vertical" 
     android:padding="2dp"> 

     <TextView 
      android:id="@+id/distric_label" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="District" 
      android:textSize="12sp" 
      android:textStyle="normal" /> 

     <EditText 
      android:id="@+id/district" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2" 
     android:orientation="vertical" 
     android:padding="2dp"> 

     <TextView 
      android:id="@+id/label_farmeragriblocks" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Agriblocks" 
      android:textSize="12sp" 
      android:textStyle="normal" /> 

     <EditText 
      android:id="@+id/agri" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" /> 

    </LinearLayout> 

</LinearLayout> 

これは私がしたいレイアウト生成します。私は今 enter image description here

をどのように巣の異なる線形レイアウトに、上記のレイアウトを追加し、私は私が開始されているものは何でも

//top layout 
     RelativeLayout newlayout = new RelativeLayout(getContext()); 
     LinearLayout belowlayout = new LinearLayout(getContext()); 
     LinearLayout above_part = new LinearLayout(getContext()); //lin with id of above_part 
     LinearLayout in_above_part_one = new LinearLayout(getContext()); //lin with id of above_part 

     //layouts properties 
     belowlayout.setOrientation(LinearLayout.HORIZONTAL); 

     //setids for the layouts 
     newlayout.setId(12); 
     belowlayout.setId(13); 

     //Button 
     Button additemsbtn = new Button(getContext()); 
     additemsbtn.setText("Log In"); 
     additemsbtn.setBackgroundColor(Color.BLACK); 

     //Username input 
     EditText username = new EditText(getContext()); 
     additemsbtn.setId(int 1); 
     username.setId(2); 

     RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT, 

     ); 

     RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT 
     ); 

     //Give rules to position widgets 
     usernameDetails.addRule(RelativeLayout.ABOVE, additemsbtn.getId()); 
     usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     usernameDetails.setMargins(0,0,0,50); 

     buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); 

が知っていることができませんので、失敗してみましたHAV

Javaの道にそれを変更したい

私はおそらく、プログラムでこのネストを行う方法に関するガイドラインが好きです。xmlファイルで生成されたものと同じものを実現することができます。

+0

これはなぜ過小評価されているのですか? –

答えて

1

少しのことができます。したがって、ネストされたレイアウトに内部アイテムを追加し、余白/パディングなどを設定するだけです。また、ビジュアル化のための背景も追加されますenter image description here

RelativeLayout root = (RelativeLayout) findViewById(R.id.root); 

    Button addButton = new Button(this); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    addButton.setLayoutParams(params); 
    addButton.setId(123); 
    root.addView(addButton); 


    LinearLayout rootLinearlayout = new LinearLayout(this); 
    RelativeLayout.LayoutParams linearRootParams = new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    linearRootParams.addRule(RelativeLayout.BELOW, 123); 
    rootLinearlayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)); 
    rootLinearlayout.setLayoutParams(linearRootParams); 

    // Nested Linears 
    LinearLayout linearOne = new LinearLayout(this); 
    LinearLayout linearTwo = new LinearLayout(this); 
    LinearLayout linearThree = new LinearLayout(this); 
    LinearLayout.LayoutParams linearParamsWithWeight = new LinearLayout.LayoutParams(
      0, 
      300); 
    linearParamsWithWeight.weight = 1; 

    linearOne.setBackgroundColor(ContextCompat.getColor(this, android.R.color.black)); 
    linearTwo.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_red_dark)); 
    linearThree.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_blue_dark)); 

    linearOne.setLayoutParams(linearParamsWithWeight); 
    linearTwo.setLayoutParams(linearParamsWithWeight); 
    linearThree.setLayoutParams(linearParamsWithWeight); 

    rootLinearlayout.addView(linearOne); 
    rootLinearlayout.addView(linearTwo); 
    rootLinearlayout.addView(linearThree);