Android -将TextView添加到自定义活动中以在多个活动中显示



我尝试将TextView添加到自定义活动中以在多个活动中显示,但没有显示任何内容。这是我的自定义活动代码:

LinearLayout layout;
public void SetupFooter() {
    layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setGravity(android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL); 
    TextView tv = new TextView(this);
    tv.setText("abc");
    layout.addView(tv);
}

这是我的main_activity xml代码,我想添加页脚:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <ImageButton
                android:id="@+id/btnGoToTop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|bottom"
                android:layout_margin="5dip"
                android:background="@drawable/buttongototop"
                android:padding="5dip"
                android:visibility="gone" />
        </FrameLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:text="TextView" />
    </LinearLayout>
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:headerDividersEnabled="false" />
    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:headerDividersEnabled="false" />
</android.support.v4.widget.DrawerLayout>

当我在扩展活动中调用SetupFooter()时,没有显示任何内容。我的代码有什么问题吗?很抱歉我的英语不好,谢谢你的帮助!

编辑:我添加了这个:addContentView(layout, new LayoutParams(LayoutParams. wrap_content,LayoutParams. wrap_content));它现在已经显示出来了,但是没有放在底部,我怎么才能让它保持在底部呢?

您在函数中创建了2个视图,一个文本视图和一个线性布局。您将文本视图添加到线性布局,但您没有将线性布局放在任何地方,因此它没有出现。如果您想将其添加到xml中已经存在的线性布局中,则需要给它一个id,以便使用findViewById获取对它的引用,然后将文本视图添加到该视图中。

最新更新