使用scrollview将xml布局多个添加到另一个xml中

时间:2014-01-19 14:08:15

标签: java android xml

我是Android世界的新手。我有一个xml布局,我想将其作为子窗口添加到其他xml文件的scrollview。

这是多次插入的布局[offerview.xml] -

enter image description here

必须插入的xml [activity_offer.xml]是 -

enter image description here

活动的Java代码是 -

public class OfferActivity extends Activity {

private static final String name = "Reebok";
private static final String content = "50% off on Reebok Shoes at Reliance Mart";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offer);

    View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.offerview, null);

    TextView offerItemTextView = (TextView) v.findViewById(R.id.offerItemTextView);
    offerItemTextView.setText(name);

    TextView offerContentTextView = (TextView) v.findViewById(R.id.offertextView);
    offerContentTextView.setText(content);

    ImageView offerImage = (ImageView) v.findViewById(R.id.offerImageView);
    offerImage.setBackgroundResource(R.drawable.ps);

    ((LinearLayout) findViewById(R.id.offersLayout)).addView(v);


    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.offer, menu);
    return true;
}

}

目前我只想尝试包含一次视图。该应用程序崩溃了。

enter image description here

enter image description here

我一直被困在这上面。

2 个答案:

答案 0 :(得分:1)

您的layout_width

中缺少layout_heightoffersLayout
...
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/offersLayout">
</LinearLayout>
...

答案 1 :(得分:1)

日志文件说你必须在activity_offer.xml中添加LinerLayout的layout_width atts。 您可以使用merge标记再次使用xml文件