2017-07-25 33 views
0

我在xml定义的布局,它看起来像这样:如何配置内部布局编程

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center"> 

     <!-- add views here -->    

    </RelativeLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/app"/> 

</LinearLayout> 

我夸大它在我的活动内,但根据从数据RelativeLayout需要有TextViews应用程序。我怎么可能扩展它并在其中添加不同的视图?


注:这是从this question不同,因为我试图找出如何在不使用类我想要添加的内容扩展一个 -layout。

回答

0

您可以简单地在代码中创建Textviews。然后将它们添加到您的版式中:

RelativeLayout innerLayout= (RelativeLayout)findViewById(R.id.inner_layout); 

TextView textView = new TextView(this); 

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

textView.setLayoutParams(p); 

innerLayout.addView(textView); 
0

的Xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:[email protected]+id/inner_layout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <!-- add views here -->    

</RelativeLayout> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/app"/> 

</LinearLayout> 

活动

RelativeLayout relativelayout=(RelativeLayout) findViewById(R.id.inner_layout); 
relativelayout.addview(//Add view here(one will be allowed)); 

如果您在订购使用RelativeLayout的规则,你的意见。最好使用LinearLayout。