Android 无法在自定义导航抽屉中设置按钮的文本

Android 无法在自定义导航抽屉中设置按钮的文本,android,layout-inflater,navigational-properties,Android,Layout Inflater,Navigational Properties,我制作了一个带有3个按钮的定制导航抽屉,我想根据与按钮相关的活动更改按钮上的文本。因此,当我使用LayoutInflater创建导航抽屉布局的java对象,并在该布局对象上使用findViewById获取每个按钮的句柄时,我设置了每个按钮的文本,但当我运行应用程序时,文本不会出现(在XML中,我没有为按钮设置任何文本)。休息一切正常 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:a

我制作了一个带有3个按钮的定制导航抽屉,我想根据与按钮相关的活动更改按钮上的文本。因此,当我使用LayoutInflater创建导航抽屉布局的java对象,并在该布局对象上使用findViewById获取每个按钮的句柄时,我设置了每个按钮的文本,但当我运行应用程序时,文本不会出现(在XML中,我没有为按钮设置任何文本)。休息一切正常

<FrameLayout 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/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sahil.childcare.NavigationalDrawerFragment">

<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <include
          layout="@layout/nav_profile"
          android:id="@+id/nav_profile"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
    <Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/nav_profile"
          android:layout_marginLeft="-5dip"
          android:layout_marginRight="-5dip"
          android:layout_marginTop="-5dip"
          android:layout_marginBottom="-5dip"
          android:id="@+id/top_button"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true" />
    <Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/top_button"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:layout_marginLeft="-5dip"
          android:layout_marginRight="-5dip"
          android:layout_marginTop="-7dip"
          android:layout_marginBottom="-5dip"
          android:id="@+id/middle_button" />
    <Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/middle_button"
          android:id="@+id/bottom_button"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          android:layout_marginLeft="-5dip"
          android:layout_marginRight="-5dip"
          android:layout_marginTop="-7dip"
          android:layout_marginBottom="-5dip"/>
</RelativeLayout>
</FrameLayout>

首先我要检查按钮是否在屏幕上可见。 为此,您可以为他们设置一些颜色背景,例如:

android:background="#ff0000"
并使用确保按钮显示在您期望的位置

接下来要检查的是,新膨胀的
片段导航抽屉是否实际用于导航抽屉,或者它可能是此布局的另一个实例,为此,您可以在膨胀代码中添加一些日志,打印您在屏幕上放置的特定实例,并在层次结构查看器中验证您在屏幕上看到的是正确的实例

更新:

为了确保答案的完整性,根据下面的注释,如果您已经通过xml添加了布局(例如使用
),而不是在代码中膨胀布局:

View view = inflater.inflate(R.layout.fragment_navigational_drawer,null);
您需要通过以下方式了解已经膨胀的布局:

View view = findViewById(R.id.my_navigational_drawer);

谢谢你的努力。当我使用XML设置文本时,它会出现。我认为这是一个不同的实例,因为当我将“onClickListener”设置为按钮以转到相应的活动时,它不会发生(甚至日志也显示了这一点,因为我在“onClickListener()”中保留了一些日志)。但是它如何能膨胀不同的实例。请同时告诉我。发布使用膨胀的
视图的代码。
你是对的抽屉有两个实例。我通过膨胀抽屉片段创建了一个,通过在xml布局中添加片段创建了一个。我所做的更改属于充气抽屉,而不是我应用程序中出现的实际抽屉。谢谢你的帮助,马尔默。
View view = findViewById(R.id.my_navigational_drawer);