来自另一个布局的 findViewById() 不更新数据

findViewById() from another layout not updating data

我有 mainActivity,其中 xml 包含另一个 xml 。 activity主布局都有一个按钮,包含的布局有另一个按钮。两者都应显示在主 activity 中。 现在,我需要更改两个按钮的文本。我可以通过 findviewByID 获得对第一个按钮的引用,并且它有效。对于进入第二个布局的第二个按钮,我通过膨胀布局来获得视图,它正确地引用了视图并且 findviewbyID 获得了对此按钮的引用。但是当改变文字时,它并没有改变。

activityMain.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
    android:orientation="vertical"
        xmlns:tools="http://schemas.android.com/tools">
<Button android:id="@+id/buttonvvv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    xmlns:android="http://schemas.android.com/apk/res/android" />
  <include
            layout="@layout/layouttest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

layouttest.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonaaa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>
---Mainactivity.java----------------------------------
 import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);

        Button b =(Button)  findViewById(R.id.buttonvvv);
        Button bb =(Button) view.findViewById(R.id.buttonaaa);
         b.setText("ssssssss");
         bb.setText("ssssssss");

    }

结果

buttonvvv text is updated and becomes ssssssss
buttonaaa text is Not updated and still Button

无需膨胀包含的布局,您可以像 b button 一样 findViewById它已包含在 activity_main.xml 布局中

替换为:

    View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
    Button b =(Button)  findViewById(R.id.buttonvvv);
    Button bb =(Button) view.findViewById(R.id.buttonaaa);
    b.setText("ssssssss");
    bb.setText("ssssssss");

与:

    Button b =(Button)  findViewById(R.id.buttonvvv);
    Button bb =(Button) findViewById(R.id.buttonaaa);
    b.setText("ssssssss");
    bb.setText("ssssssss");

来自文档include:

The root View should be exactly how you'd like it to appear in each layout to which you add this layout.

顺便说一句, 检查这个 answer

您不必扩充布局,只需找到它即可:

LinearLayout layout =(LinearLayout)  findViewById(R.id.layouttest);

然后

Button bb =(Button) layout.findViewById(R.id.buttonaaa);