1

我制作了一个带有 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>

涉及此抽屉的主要活动(onCreate()方法内部)中的代码部分在这里

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.fragment_navigational_drawer,null);
    Button topButton = (Button) view.findViewById(R.id.top_button);
    Button middleButton = (Button) view.findViewById(R.id.middle_button);
    Button bottomButton = (Button) view.findViewById(R.id.bottom_button);
    topButton.setText("School");
    topButton.setTextColor(getResources().getColor(R.color.red));
    middleButton.setText("Teacher");
    middleButton.setTextColor(getResources().getColor(R.color.red));
    bottomButton.setText("Child");
    bottomButton.setTextColor(getResources().getColor(R.color.red));
4

1 回答 1

1

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

android:background="#ff0000"

并且还使用hierarchy-viewer来确保您的按钮出现在您希望它们出现的位置。

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

更新:

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

View view = inflater.inflate(R.layout.fragment_navigational_drawer,null);

您需要通过以下方式处理已经膨胀的布局:

View view = findViewById(R.id.my_navigational_drawer);
于 2016-11-28T09:46:34.933 回答