我想用编程方式填充ViewFlipper这样的布局:
flippercontainer.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"
android:weightSum="1" >
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_weight="0.3"
android:padding="10dp"
android:src="@drawable/breakfast" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.7"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Name of product"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="40dp"
android:text="Cafeteria" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Delivery" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtCaf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="40dp"
android:text="$" />
<TextView
android:id="@+id/txtDel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="$" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
但是,我想更改ImageView的背景,以及具有id的TextView的文本。 如果我这样尝试,我会得到NullPointerException:
LayoutInflater inflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.flippercontainer, null );
name=(TextView)view.findViewById(R.id.txtName);
name.setText("View number "+ i);
在最后一行。 如何以编程方式填充ViewFlipper,并更改我想要的值?提前谢谢。
答案 0 :(得分:3)
每个浏览器的布局都与此相同 - flippercontainter.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"
android:weightSum="1" >
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:padding="10dp" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Name of product"
android:textColor="#31B6E7"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtCaf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="40dp"
android:text="$" />
</LinearLayout>
使用viewflipper进行活动:
private void setFlipperContent() {
List<Stuff> aux=getStuffList();
end = aux.size();
for (int i = 0; i < end; i++) {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.flippercontainer, null);
flipper.addView(view);
image = (ImageView) view.findViewById(R.id.imgView);
image.setBackgroundResource(aux.get(i).getImg());
name = (TextView) view.findViewById(R.id.txtName);
name.setText(aux.get(i).getName());
cafPrice = (TextView) view.findViewById(R.id.txtCaf);
cafPrice.setText("$" + aux.get(i).getCafPrice());
}
setFlipperAnimation();
}