![](/img/trans.png)
[英]Android layout: How to put 2 TextViews next to each other with minimum space
[英]how to put TextViews next to each other
我想創建一些TextViews並將它們彼此相鄰,如果TextViews到達頁面末尾,則從以前的TextViews的下面開始。 我編寫了將TextViews彼此並排放置的代碼,如下所示:
RelativeLayout main = (RelativeLayout) inflater.inflate(R.layout.activity_main, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<10; i++){
TextView text = new TextView(this);
text.setText("Text"+String.valueOf(i));
text.setId(i);
main.addView(text, params);
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, text.getId());
}
但是當TextViews到達屏幕末端時,它類似於以下內容:
我怎么解決這個問題?
我認為最優雅的方法是使用RecyclerView之類的適配器視圖。 您可以在每一行中定義最大項目數。 它的工作方式類似於ListView,但具有更多功能。 在此處閱讀: https : //developer.android.com/training/material/lists-cards.html http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.