【问题标题】:Adding TextView to content_main in Navigation Drawer Template在导航抽屉模板中将 TextView 添加到 content_main
【发布时间】:2017-02-01 02:26:06
【问题描述】:

我正在尝试在 setContentView 位于 activity_main 时将 TextView 添加到 content_main 布局。当您在 Android Studio 中创建新项目时,我目前正在使用 Navigation Drawer 模板。 Activity_main 包含 app_bar_main,其中 app_bar_main 包含 content_main。

Content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="org.test.testy.test"
    tools:showIn="@layout/app_bar_main"
    android:weightSum="1">

</LinearLayout>

我正在尝试使用的代码是这样的:

    LayoutInflater inflater;
    inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = (View) inflater.inflate(R.layout.content_main, null);
    LinearLayout layout = (LinearLayout) view.findViewById(R.id.content_main);
    //Card view create
    CardView cv = new CardView(layout.getContext());
    cv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    cv.setVisibility(View.VISIBLE);
    TextView tv = new TextView(layout.getContext());
    tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    tv.setText("Test");
    tv.setVisibility(View.VISIBLE);
    cv.addView(tv);
    layout.addView(cv);

//Card view create 下面的所有内容都在一个空白应用程序中进行了测试,效果非常好。我还尝试在 content_main 的 xml 中放置一个带有 id 的 TextView 并尝试读取文本,该文本也返回了正确的文本。如果我能够访问布局并且添加到视图的代码很好,那为什么它不起作用?

【问题讨论】:

  • 在您的第二个代码块中,您正在扩充 content_main 布局的新实例。如果您不将其添加到屏幕上的View 层次结构中,您将看不到它。也就是说,您不会在该代码中的任何位置将layout 添加到Activity
  • 那么我需要在代码中添加什么?
  • 我假设你实际上并不想膨胀一个新的content_main,所以删除所有LayoutInflater 的东西,并在Activity 中找到layout,就像你做任何事情一样其他View。即LinearLayout layout = (LinearLayout) findViewById(R.id.content_main);
  • 这行不通,它只是说尝试在空对象引用上调用虚拟方法 'void android.widget.LinearLayout.addView(android.view.View)'
  • 好吧,听起来您要么在调用 setContentView() 之前尝试这样做,要么您的布局设置与您描述的不完全一样。

标签: android


【解决方案1】:

我做错了两件事:

  1. 我必须为每个&lt;include&gt;s 加上它要去的确切名称,并且

  2. 我必须检查每个包含,因为我有两个:

    LinearLayout layout = (LinearLayout) findViewById(R.id.app_bar_main).findViewById(R.id.content_main);
    

【讨论】:

  • 如何回到 content_main 布局?我的意思是在我们从抽屉中选择一个项目之后,新的片段就会出现在它的位置。如何回到 content_main 布局?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2015-03-23
  • 2020-03-12
相关资源
最近更新 更多