我有一個BaseLayout
,BaseLayout
是一個ParentLayout
其他佈局。換句話說,其他佈局繼承/包含它。 BaseLayout
有DrawerLayout
(共享所有佈局),但每個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
元素融入BaseLayout
LinearLayout
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>
這會幫助你:https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view您可能需要進一步搜索'Android的動態添加view' 。 –
可能重複的[Android - 動態添加視圖到視圖](https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) –