我的布局包含多个线性布局。我想点击子布局时,更改其高度以匹配父级。我尝试了这段代码,但点击它时不会扩展布局:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.activity_main,
null);
for (int i = 0; i < 1; i++) {
View custom = inflater.inflate(R.layout.row, null);
TextView tv = (TextView) custom.findViewById(R.id.text);
tv.setText("Custom View " + i);
parent.addView(custom);
}
setContentView(parent);
View row = inflater.inflate(R.layout.row, null);
final LinearLayout ro = (LinearLayout) findViewById(R.layout.row);
row.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ro.getLayoutParams().height = 100;
}
});
}
和activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
和row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.activity_main,
null);
for (int i = 0; i < 1; i++) {
View custom = inflater.inflate(R.layout.row, null);
custom.setTag(i);
TextView tv = (TextView) custom.findViewById(R.id.text);
tv.setText("Custom View " + i);
parent.addView(custom);
}
setContentView(parent);
for (int i = 0; i < 1; i++) {
View row = findViewByTag(i);
row.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ro.getLayoutParams().height = 100;
}
});
}
}
注意:我没有测试过它..plz解决了小错误,如果有的话