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

时间:2014-06-27 19:09:02

标签: android

我尝试将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)); 它现在显示但它没有放在底部,我怎么能让它留在底部?

1 个答案:

答案 0 :(得分:0)

您在函数,文本视图和线性布局中创建了2个视图。您正在将文本视图添加到线性布局中,但您没有将线性布局放在任何位置,因此它不会出现。如果你想将它添加到xml中已经存在的线性布局中,你需要给它一个id,这样你就可以用findViewById获得它的引用,然后将文本视图添加到它。