gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:07:13 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com