【问题标题】:Android - Add TextView to custom activity to display in multiple activitiesAndroid - 将 TextView 添加到自定义活动以在多个活动中显示
【发布时间】:2014-06-28 09:11:48
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

    【讨论】:

    • 嗨,我添加了这个 addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));它现在显示了,但它不在底部,它在顶部..
    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多