簡體   English   中英

使用滾動視圖將xml布局倍數添加到另一個xml中

[英]Add a xml layout multiple into another xml with scrollview

我是android世界的新手。 我有一個XML布局,我想作為子級添加到其他xml文件的scrollview中。

這是要插入多次的layout [offerview.xml]-

在此處輸入圖片說明

必須將其插入的xml [activity_offer.xml]是-

在此處輸入圖片說明

活動的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;
}

}

此刻,我只是試圖只包含一次視圖。 該應用程序崩潰了。

在此處輸入圖片說明

在此處輸入圖片說明

我一直被這個問題困擾。

您在offersLayout中缺少layout_widthlayout_height

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

日志文件說您必須在activity_offer.xml中添加LinerLayout的layout_width屬性。 您可以使用合並標記再次使用xml文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM