2016-09-23 106 views
-1

我想与他们之间嵌套的线性布局相对布局,但我无法理解嵌套如何虐待他们编程设置叠起来的布局以编程方式在Android的

这是我为我的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

现在我想补充上述布局,将其更改为一个java方式

我甲肝试过,因为我无法

//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); 

无论我已经开始不知道如何嵌套不同的线性布局,

我可能会喜欢有关如何以编程方式进行嵌套的指南,以便我可以实现由xml文件生成的相同内容

+0

为什么这是undermarked –

回答

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); 
相关问题