android - addView 将文本推送到抽屉布局下方

标签 android

我是 android 应用程序开发的新手,我遇到了一个问题:

当按下我的 DrawerLayout 上的按钮时,我正在向我的主 LinearLayout 添加内容:

private void switchTo(String text) {
    final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(params);
    textView.setText("New text: " + text);
    content.addView(textView, 0);
}

但这会将文本添加到我的 LinearLayout 之上:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_layout"
android:gravity="bottom">

<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:background="#e6e6e6"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/navList"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#cccccc"/>
</android.support.v4.widget.DrawerLayout>

我如何设法在 DrawerLayout 之后添加文本,因为现在添加文本会将 DrawerLayout 向下推(如果不清楚,我希望 DrawerLayout 在高度上保持在页面顶部,同时呈现文本“旁边")

最佳答案

DrawerLayout 是这样工作的

To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.

Check this information regarding DrawerLayouts

你的布局文件应该有 2 个 View :
1 - 主要内容(抽屉关闭时)
2 - 抽屉本身

我希望你的布局是这样的

<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">

    <!-- The main content view : here you define the layout of your activity -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <!-- The main content view : here you define the layout of your activity -->
    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 

        <!-- The navigation drawer -->
        <ListView
            android:id="@+id/navList"
            android:layout_width="250dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="#cccccc"/>

    <LinearLayout/>

</android.support.v4.widget.DrawerLayout>

此外,当您指定:

 content.addView(textView, 0);

您严格来说是想将 textView 添加到内容的第一个位置。 这会在每个其他之前添加您的 textView,导致 textView 在 drawerLayout 之前 Check this reference regarding ViewGroups and adding views

所以,修改后的代码是

..
private LinearLayout contentFrame;
..
contentFrame = (LinearLayout) findViewById(R.id.content_frame);
...

private void switchTo(String text) {
    final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(params);
    textView.setText("New text: " + text);
    contentFrame.addView(textView);
}

关于android - addView 将文本推送到抽屉布局下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38785481/

相关文章:

android - proguard 混淆后,应用程序在 asynctask(服务器 api 调用)中崩溃

android - 动态 fragment 中的布局

android - 如何在另一个Fragment中注销Google身份验证?

java - Android 依赖库中缺少 facebooksdk.jar

java - 以正确的方式编码。 Android onCreate 和初始化

android - 无法在 TimePickerDialog 中动态选择 AM/PM

Android ClickableSpan 不调用 onClick

android - 如何根据手机的mdpi、hdpi和屏幕大小调整位图图像的大小

android - ImageView 中比例为 3 的图像 :2

android - 如何在 Android 中以编程方式关闭 "Ask to use WLAN"?