我通过预定义的XML在android中生成动态视图。该守则如下。我可以使用card_identifier sting为不同布局中的每个元素分配动作。问题是我无法访问动态布局中的元素,例如如果我想用sw1.setCheched(true)在动态视图1中设置开关1,那么如何处理该元素?
代码:
public void threeSwitchLayout(String card_name, String card_identifier) {
// Generate dynamic layout
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Use demo.xml to create layout
final View addView = layoutInflater.inflate(R.layout.demo, null);
final TextView cardTitle = (TextView)addView.findViewById(R.id.cardTitle);
cardTitle.setText(card_name);
final TextView cardIdentifier = (TextView)addView.findViewById(R.id.cardIdentifier);
cardIdentifier.setText(card_identifier);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 15);
addView.setLayoutParams(layoutParams);
SwitchCompat sw1 = (SwitchCompat)addView.findViewById(R.id.switch1);
SwitchCompat sw2 = (SwitchCompat)addView.findViewById(R.id.switch2);
SwitchCompat sw3 = (SwitchCompat)addView.findViewById(R.id.switch3);
sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW1 " + isChecked, Toast.LENGTH_SHORT).show();
}
});
sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW2 " + isChecked, Toast.LENGTH_SHORT).show();
}
});
sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW3 " + isChecked, Toast.LENGTH_SHORT).show();
}
});
container.addView(addView);
}
demo.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/rounded_corners"
android:layout_margin="5dp"
android:layout_marginBottom="5dp"
android:padding="5dp">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardTitle"
android:textStyle="normal|bold"
android:textSize="25sp"
android:padding="5dp"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:layout_weight="1" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cardIdentifier"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/switch1"
android:text="SW 1 "
android:theme="@style/SCBSwitch" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/switch2"
android:text="SW 2 "
android:theme="@style/SCBSwitch"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/switch3"
android:text="SW 3 "
android:theme="@style/SCBSwitch"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:1)
请查看以下代码段
<强> MainActivity 强>
public class Main2Activity extends AppCompatActivity {
LinearLayout container;
View addView;
RelativeLayout switchLay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
container = (LinearLayout) findViewById(R.id.container);
}
public void addLayout(View v) {
threeSwitchLayout("Try", "first");
}
public void ChangeState(View v) {
try {
SwitchCompat toChng = (SwitchCompat) switchLay.findViewById(R.id.switch1);
toChng.setChecked(true);
} catch (NullPointerException npe) {
Toast.makeText(this, "please add view first", Toast.LENGTH_SHORT).show();
}
}
public void threeSwitchLayout(String card_name, String card_identifier) {
// Generate dynamic layout
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Use demo.xml to create layout
addView = layoutInflater.inflate(R.layout.demo, null);
final TextView cardTitle = (TextView) addView.findViewById(R.id.cardTitle);
cardTitle.setText(card_name);
final TextView cardIdentifier = (TextView) addView.findViewById(R.id.cardIdentifier);
cardIdentifier.setText(card_identifier);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 15);
addView.setLayoutParams(layoutParams);
switchLay = (RelativeLayout) addView.findViewById(R.id.switchLay);
SwitchCompat sw1 = (SwitchCompat) addView.findViewById(R.id.switch1);
SwitchCompat sw2 = (SwitchCompat) addView.findViewById(R.id.switch2);
SwitchCompat sw3 = (SwitchCompat) addView.findViewById(R.id.switch3);
sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW1 " + isChecked, Toast.LENGTH_SHORT).show();
}
});
sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW2 " + isChecked, Toast.LENGTH_SHORT).show();
}
});
sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW3 " + isChecked, Toast.LENGTH_SHORT).show();
}
});
container.addView(addView);
}
}
activity_main2.xml(MainActivity的布局文件)
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="addLayout"
android:text="add layout" />
<Button
android:id="@+id/btnChangeState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="ChangeState"
android:text="Change switch state" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnAdd"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
<强>释强> 我所做的是,我只是给交换机视图的父布局提供了一个ID,然后在全局的java类中引用它们(RelativeLayout switchLay),类似地声明了你的&#34; addview&#34;还可以通过这些全局视图访问我们想要更新的开关。
虽然这只是一个片段,但您可能需要根据您的具体要求更新代码。