Изменить TextView другого файла макета - PullRequest
0 голосов
/ 16 апреля 2020

Как я могу изменить TextView по идентификатору другого файла LayOut?

Вот код: https://pastebin.com/BDs4H3uE

Вот сценарий:

Я создал меню ящика, вот макет XML:

activity_main. xml:

<com.google.android.material.navigation.NavigationView
    android:layout_width="277dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"
    app:headerLayout="@layout/header"
    />

hook_menu. xml внутри папки меню [Меню слайдера]

right_menu. xml внутри папки меню [Значок правой стороны, содержащий значок уведомления]

Внутри right_menu. xml, я добавил файл макета:

<item
        android:title="Notifications"
        android:id="@+id/notification_icon"
        app:showAsAction="ifRoom"
        app:actionLayout="@layout/bell"/>

Файл макета колокольчика содержит значок колокольчика и TextView с идентификатором (@ + id / messages_num), показывающим количество уведомлений в цифрах.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@android:style/Widget.ActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/right_menu"
    android:clickable="true"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hotlist_bell"
        android:src="@drawable/bell_icon"
        ></ImageView>

    <TextView
        android:id="@+id/notification_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/hotlist_bell"
        android:layout_alignLeft="@+id/hotlist_bell"
        android:layout_alignTop="@+id/hotlist_bell"
        android:layout_alignRight="@id/hotlist_bell"
        android:layout_marginStart="13dp"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="-3dp"
        android:layout_marginRight="17dp"
        android:background="@drawable/rounded_square"
        android:gravity="center"
        android:paddingLeft="4dp"
        android:paddingRight="5dp"
        android:paddingBottom="1dp"
        android:text=""
        android:textColor="#ffffffff"
        android:textSize="12sp" />
</RelativeLayout>

Проблема:

Я хочу динамически изменять текст TextView из файла MainActivity. java. Например, текст @ + id / messages_num

1 Ответ

0 голосов
/ 16 апреля 2020

В основном я хотел изменить TextView пункта меню, вот код, который мне помог:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.right_menu,menu);
    TextView textView = (TextView) menu.findItem(R.id.notification_icon).getActionView().findViewById(R.id.hotlist_hot);
    textView.setText("11");

    return super.onCreateOptionsMenu(menu);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...