1

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));
4

1 に答える 1

1

最初に、ボタンが画面に表示されているかどうかを確認します。これを行うには、いくつかの色の背景を設定できます。たとえば、次のようになります。

android:background="#ff0000"

また、hierarchy-viewerを使用して、ボタンが表示される場所にボタンが表示されるようにします。

次に確認することは、新しくインfragment_navigational_drawerフレートされたものが実際にナビゲーション ドロワーに使用されているかどうか、またはこのレイアウトの別のインスタンスであるかどうかです。これを行うには、インフレーション コードにログを追加して、画面に配置した特定のインスタンスを出力します。階層ビューアで確認すると、画面に正しいインスタンスが表示されます。

アップデート:

回答を完全にするために、以下のコメントに従って<include>、コードでインフレートするのではなく、xml を介してレイアウトを既に追加している場合 (例: を使用):

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

次のような方法で、すでに膨張したレイアウトのハンドルを取得する必要があります。

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