【问题标题】:LayoutInflater not Updating the View PropertyLayoutInflater 不更新视图属性
【发布时间】:2014-02-03 09:02:05
【问题描述】:

我只是使用 LayoutInflater 从除 activity_main.xml 之外的 layout.xml 文件中获取视图(按钮)。但是当我更改按钮的 setText("") 属性时,按钮属性不会更新。

我正在检查是否可以更改 testlayout.xml 中按钮元素的文本属性但是当我在 onCreate 中设置 Button 的属性并且当我检查调用中未更改的属性时...

MainActivity.java

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LayoutInflater inflater = (LayoutInflater)this.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);


        LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.testlayout, null);
        Button btn = (Button)linearLayout.findViewById(R.id.testbtn);
        btn.setText("Hello");
    }


    public void call(View v){

        Button btn  = (Button)findViewById(R.id.testBtn);
        Toast.makeText(this, btn.getText(), Toast.LENGTH_SHORT).show();

    }

Activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/mainBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="73dp"
        android:text="Button"
        android:onClick="call"
         />

</RelativeLayout>

testlayout.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"
    android:orientation="vertical" >

    <Button
        android:id="@+id/testBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

【问题讨论】:

  • 你在哪里将错误的布局添加到活动中?
  • 我正在检查是否可以更改 testlayout.xml 中按钮元素的文本属性但是当我在 onCreate 中设置按钮的属性以及在调用中检查该属性时不是变了……
  • 但除非您将其添加到活动中,否则您不会看到按钮文本发生变化
  • 在调用(查看 v)函数中我正在检查更改属性..
  • 跟随 vipul; 的回答并尝试

标签: java android xml android-layout


【解决方案1】:

您需要将此线性布局添加到您的活动中:

 LayoutInflater inflater = (LayoutInflater)this.getSystemService
              (Context.LAYOUT_INFLATER_SERVICE);


    LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.testlayout, null);
    Button btn = (Button)linearLayout.findViewById(R.id.button1);
    btn.setText("Hello");

    RelativeLayout rl=findViewById(R.id.root);
    rl.addView(linearLayout);

【讨论】:

    【解决方案2】:

    您需要使用 ViewGroup.addView(View child) 方法将 testlayout.xml 作为子项添加到活动视图中。目前,您会看到第一个布局中的按钮。你想做什么?

    【讨论】:

      【解决方案3】:

      您需要将其他 xml 添加到当前视图中:-

          package com.example.testdynamicviews;
      
      import android.app.Activity;
      import android.graphics.Color;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.Menu;
      import android.view.View;
      import android.widget.Button;
      import android.widget.LinearLayout;
      import android.widget.TextView;
      
      public class MainActivity extends Activity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              LinearLayout main = (LinearLayout)findViewById(R.id.mainView); // Your custom view will be added in this layout
      
              TextView text = myView();
              text.setText("Rahul GuptaRahul GuptaRahul GuptaRahul GuptaRahul GuptaRahul GuptaRahul Gupta");
              main.addView(text); // You forgot to add this line
      
              Button button = myCustomButton();
              button.setText("Custom Button");
              main.addView(button);
          }
      
          public TextView myView(){
              View v;
              LayoutInflater li = LayoutInflater.from(getBaseContext());
              v = li.inflate(R.layout.textview, null);
              int width = (int) (getResources().getDimension(R.dimen.testwidth)/ getResources().getDisplayMetrics().density);
              v.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.WRAP_CONTENT));
              return (TextView) v;
          }
      
          public Button myCustomButton(){
              View v;
              LayoutInflater li = LayoutInflater.from(getBaseContext());
              v = li.inflate(R.layout.custombutton, null);
              v.setBackgroundColor(Color.RED);
              int width = (int) (getResources().getDimension(R.dimen.testwidth)/ getResources().getDisplayMetrics().density);
              LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
              params.setMargins(0, 10, 0, 0);
              v.setLayoutParams(params);
              return (Button) v;
          }
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
          }
      
      }
      

      textview.xml

      <?xml version="1.0" encoding="utf-8"?>
      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
          android:text="sgfiughbjkh"
          android:singleLine="true"
          android:textSize="@dimen/testsize"
          android:background="@android:color/holo_blue_bright"
          android:ellipsize="end" >
      
      </TextView>
      

      自定义按钮.xml

          <?xml version="1.0" encoding="utf-8"?>
      <Button xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:color/holo_blue_bright"
          android:text="sgfiughbjkh"
          android:textSize="@dimen/testsize" />
      

      【讨论】:

        猜你喜欢
        • 2023-01-26
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        • 1970-01-01
        • 1970-01-01
        • 2020-04-08
        • 2021-03-08
        • 2020-09-21
        相关资源
        最近更新 更多