我已经制作了一个带有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] 删除。
我来说两句