2017-07-25 61 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。