[英]How to add layout.xml to another layout.xml programmatically
[英]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_width
和layout_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.