2017-07-01 37 views
0

我有一個BaseLayout,BaseLayout是一個ParentLayout其他佈局。換句話說,其他佈局繼承/包含它。 BaseLayoutDrawerLayout(共享所有佈局),但每個Activity有不同的內容。如何以編程方式在包含的佈局中添加TextView?

我的baselayout看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 
    <android.support.v4.widget.DrawerLayout 
    android:id="@+id/myDrawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!--Main Content--> 
    <LinearLayout 
     android:id="@+id/rootLinearLayout" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:orientation="vertical"> 
     <!--Content--> 

    <!--must add Other Layout elements there 
    must add Other Layout elements there 
    must add Other Layout elements there 
    must add Other Layout elements there 
    must add Other Layout elements there !--> 

    </LinearLayout> 
    <!-- Left Navigation Derawer --> 
    <LinearLayout 
     android:id="@+id/llLeftDrawer" 
     android:orientation="vertical" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:background="?attr/colorAccent" 
     android:padding="15dp" 
     android:layout_gravity="right"> 
     <!-- Left Navigation Derawer Content--> 
     <TextView 
     android:text="Categories" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView1" 
     android:gravity="center" 
     android:textColor="#FFFFFF" 
     fontPath="fonts/MobileFont.ttf" /> 
     <ListView 
     android:id="@+id/lvLeftMenu" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" /> 
    </LinearLayout> 
    </android.support.v4.widget.DrawerLayout> 
</LinearLayout> 

並列入Postlayout像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
    <include 
    layout="@layout/BaseLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

如何添加PostLayout元素融入BaseLayoutLinearLayout ID rootLinearLayout

postLayout元件是這樣的:所有的

<LinearLayout 
       android:orientation="vertical" 
       android:minWidth="25px" 
       android:minHeight="25px" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/linearLayout1"> 
       <TextView 
        android:text="Large Text" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/txtPostTitle" 
        android:gravity="right" /> 
       <TextView 
        android:text="Small Text" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/txtPostDate" 
        android:gravity="right" /> 
       <android.webkit.WebView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/wbvPostContent" /> 
      </LinearLayout> 
+1

這會幫助你:https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view您可能需要進一步搜索'Android的動態添加view' 。 –

+0

可能重複的[Android - 動態添加視圖到視圖](https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) –

回答

0

布魯諾BIERI得到solution Java和我轉換成xamarin:

LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater; 
View v = inflater.Inflate(Resource.Layout.your_layout, null); 

// fill in any details dynamically here 
TextView textView = v.FindViewById<TextView>(Resource.Id.a_text_view); 
textView.Text="your text"; 

// insert into main view 
ViewGroup insertPoint = FindViewById<ViewGroup>(Resource.id.insert_point); 
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

和羅漢斯塔克解決好訪問

tanQ布魯諾BIERI和羅漢斯塔克包括佈局的元素,

0

首先,提供在Postlayoutid屬性到包含Baselayout

然後,您可以通過調用具有Postlayout活動如下得到來自包括視圖View對象: -

View includedView = (View)findViewById(R.id.idThatYouGaveToIncludedLayout); 

然後,您可以從includedView對象訪問所有Baselayout意見和執行您需要的操作。

例如,

LinearLayout rootLinearLayout = (LinearLayout)includedView.findViewById(R.id.rootLinearLayout); 

上面的代碼類似,您可以獲取你的Baselayout的其他視圖對象。

+0

我需要添加'postLayout'元素到'當從BaseLayout包含時,BaseLayout只有'DrawerLayout',但每個活動都有不同的內容,例如PostDetailsActivity有兩個textview和一個webview。其他活動'MainActivity'有一個'RecyclerView'和其他活動...。我想要做什麼所有活動的佈局包括'BaseLayout'之後,以編程方式將每個活動的元素添加到他自己的佈局'rootLinearLayout' – arman

0

當您將視圖添加爲包含視圖時,可以像使用自己的主視圖一樣使用所有視圖。找到佈局作爲視圖並添加視圖。如下圖所示:

LinearLayout linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1); 

linearLayout1.addView(new TextView(context)); 
相關問題