我只是使用LayoutInflater从一个layout.xml文件中获取A View(Button),而不是activity_main.xml。但是当我更改按钮的setText(“”)属性时,按钮属性不会更新。
我正在检查是否可以更改testlayout.xml中button元素的text属性但是当我在onCreate中设置Button的属性时以及当我在调用中检查那个未被更改的属性时... < / p>
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>
答案 0 :(得分:0)
您需要使用ViewGroup.addView(View child)方法将testlayout.xml作为子项添加到活动视图中。目前,您可以看到第一个布局中的按钮。你想做什么?
答案 1 :(得分:0)
您需要将此linearlayout添加到您的活动中:
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 :(得分:0)
您需要将其他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>
custombutton.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" />