0

我有一个 viewflipper,我想多次填充相同的布局,但在每个布局中,我需要根据某个数组显示不同的文本和不同的背景图像。到目前为止,它只是每次都用不同的文本/背景替换第一个视图。现在,我已经预定义了数组。任何帮助是极大的赞赏。

取景器的代码:

private int numcards = 3;
private String creditnum[] = {"***********2451", "***********2452", "***********2453"};
private int carddraw[] = {R.drawable.fullcredit_blue, R.drawable.fullcredit_green, R.drawable.fullcredit_silver};

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.show_cards);
    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);

    gestureDetector = new GestureDetector(this);
    for (int i = 0; i < numcards; i++)
    {
        viewFlipper.addView(View.inflate(this, R.layout.card_scroll_item, null), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/credit.otf");
        Button creditbtn = (Button) findViewById(R.id.credit_button);
        creditbtn.setBackgroundResource(carddraw[i]);
        TextView txtname = (TextView) findViewById(R.id.credit_type);
        txtname.setText("Visa");
        TextView txtnumber = (TextView) findViewById(R.id.credit_number);
        txtnumber.setText(creditnum[i]);
        txtnumber.setTypeface(tf);

    }
}

我正在膨胀的视图的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


<Button
    android:id="@+id/credit_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="104dp" android:layout_marginBottom="220dp" android:layout_alignParentBottom="true"/>

<TextView
    android:id="@+id/credit_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/credit_button"
    android:layout_alignTop="@+id/credit_button"
    android:layout_marginRight="22dp"
    android:layout_marginTop="21dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" android:typeface="normal"/>



<TextView
    android:id="@+id/credit_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/credit_button"
    android:layout_alignRight="@+id/credit_type"
    android:layout_centerVertical="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" android:layout_marginLeft="15dp" android:layout_marginBottom="30dp"/>

4

1 回答 1

0

问题是您没有保留要添加的视图,因此在 Activity 的内容视图上调用 findViewById,它将始终返回具有该 ID 的第一个视图。试试这个:

View view = View.inflate(this, R.layout.card_scroll_item, null);
viewFlipper.addView(view);
Button creditbtn = (Button) view.findViewById(R.id.credit_button);
creditbtn.setBackgroundResource(carddraw[i]);
TextView txtname = (TextView) view.findViewById(R.id.credit_type);
txtname.setText("Visa");
TextView txtnumber = (TextView) view.findViewById(R.id.credit_number);
txtnumber.setText(creditnum[i]);
txtnumber.setTypeface(tf);

这些布局参数是多余的,因为您在 XML 中定义了它们。

于 2012-06-22T07:11:30.730 回答