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

海滩

我已经制作了一个带有3个按钮自定义导航抽屉,我想根据它们与之关联的活动来更改按钮上的文本。因此,当我使用LayoutInflater创建导航抽屉的布局的Java对象并使用该布局对象上的findViewById获取每个按钮的句柄时,我设置了每个按钮的Text,但是当我运行该应用程序时,该文本不会出现(在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));
大理石

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

android:background="#ff0000"

并使用分层查看器确保您的按钮出现在您希望它们出现的位置。

接下来要检查的是,新充气的飞机fragment_navigational_drawer实际上是否已用于导航抽屉,或者它可能是此布局的另一个实例,为此,您可以在充气代码中添加一些日志记录,打印您放在屏幕上的特定实例,并在hierarchy-viewer中进行验证,您会在屏幕上看到正确的实例。

更新:

为了使答案更完整,请按照以下注释,如果您已经通过xml添加了布局(例如使用<include>),而不是在代码中添加布局

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

您需要通过以下方式获取已经膨胀的布局的句柄:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SwiftUI中NavigationView导航栏的自定义后退按钮

如何创建自定义导航抽屉,该抽屉将在抽屉onitemclick旁边打开ListView?

在UINavigationController中设置自定义导航栏类

自定义导航抽屉上的波纹效果

无法使用自定义CSS Mixins在Polymer 1.0中为纸抽屉面板设置样式

如何创建自定义导航抽屉?

Android Jetpack导航-带抽屉项的自定义操作

自定义导航抽屉中setOnClickListener的最佳做法

如何在导航抽屉中设置自定义图标?

反应本机自定义抽屉导航

无法从自定义标题切换堆栈导航器中的抽屉

将自定义图标添加到抽屉导航

带自定义按钮的导航抽屉

无法设置自定义按钮的样式

如何在抽屉导航器中添加自定义抽屉,反应导航4.x?

如何使用React Navigation v.5中的DrawerItemList在抽屉式导航器中传递自定义按钮?

反应导航中的自定义抽屉

如何在Android中创建自定义导航抽屉

添加自定义导航抽屉操作栏图标

处理自定义列表项在导航抽屉ListView中单击

带有活动和片段的自定义导航抽屉

如何在android中制作自定义导航抽屉

自定义 Listview 不显示在导航抽屉的片段中,具有自定义 arrayadaptar 类

如何在导航抽屉中居中自定义布局?

如何使用基于抽屉导航的自定义组件从侧边菜单中获取价值

React Native 抽屉导航:如何通过单击特定场景中的自定义按钮打开抽屉?

无法执行单击导航栏自定义按钮

无法在 React Native 中的自定义后退按钮上运行反应导航功能

我的自定义抽屉导航工具栏中的错误是什么?