[英]Adding TextViews inside horizontal LinearLayout dynamically
在我的應用程序的配置文件頁面中,我想要一個感興趣的部分,如圖所示。 用戶在其個人資料下有一個興趣列表。 我想在水平LinearLayout中顯示他/她的興趣。 我已經創建了一個TextViews數組,並在父級LinearLayout中動態添加它們,但是當沒有更多空間時,我不想添加TextViews。 相反,我想添加一個TextView來顯示剩余興趣的數量。
如圖所示(使用圖像鏈接),用戶有24個興趣點,其中4個水平排列在同一行上,最后一個TextView(+20)顯示同一行上剩余的興趣點數。
String interestList[]={"Travel","Music","Photography","Sports","Dance","Animals","SciFi Movies"};
int interestWidth =0, parentWidth=interestLinearLayout.getWidth();
for(String interest: interestList) {
TextView textView = new TextView(MainActivity.this);
textView.setBackground(getResources().getDrawable(R.drawable.interests_bg));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(2,0,10,2);
textView.setLayoutParams(params);
textView.setPadding(2, 2, 2, 2);
textView.setText(" "+interest+" ");
textView.setTextColor(getResources().getColor(R.color.white));
textView.setIncludeFontPadding(true);
interestWidth += textView.getWidth();
if(interestWidth<parentWidth) //both are 0 on first iteration of loop???
interestLinearLayout.addView(textView);
else
break;
}
您可以動態添加視圖,但首先需要引用要向其添加視圖的父視圖。
您可以只使用findViewById來做到這一點。 假設它是線性布局,
LinearLayout parent = findViewById(R.id.parent);
// Then create a textview
TextView textView = new TextView(this);
// Add the view to the parent
parent.addView(textView);
就是這樣! 若要更改有關TextView的屬性,可以使用TextView getter和setter。 如果要更改TextView的邊距,邊距或寬度的高度,請使用LayoutParams
// Remember that I'm using LinearLayout.LayoutParams because the parent of the ttextview is a LinearLayout
LinearLayout.LayourParams params = textView.getLayoutParams();
// Remember these values are in pixels
params.height = 100;
params.width = 200;
使用此方法存在很多問題,例如以像素而不是dps設置高度和寬度。 當您可以在xml中完成代碼時,請編寫大量代碼。 但是,通過在res / layout中創建一個xml文件,然后對其進行充氣並最終將其添加到父級,可以使此過程變得更加容易。
您可以通過-
// First get the layout inflater
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView textView = inflater.inflate(R.layout.myTextView, null);
linearLayout.addView(textView);
最后解決您的問題,即僅添加足夠的視圖以使linearLayout不會超出屏幕寬度。
最簡單的解決方案是遍歷興趣列表,並在循環的每次迭代中,測量創建的TextView的組合寬度,然后檢查其是否超過linearLayout的寬度。
看起來與此類似-
int combinedWidth = 0;
int linearLayoutWidth = linearLayout.getMeasuredWidth();
for(String interest : interests){
TextView view = inflater.inflate(R.layout.textview, null);
combinedWidth += textView.getMeasuredWidth();
view.setText(interest);
if(combinedWidth > linearLayoutWidth){
// No need to add more views
break;
}else{
linearLayout.addView(textView);
}
}
但是,根據何時執行上述解決方案,它可能會起作用,也可能不會起作用。 因此,將活動代碼與xml文件一起發布,以便我更好地回答您的問題。
interestWidth和parentWidth最初為0,因為在調用getWidth時尚未布置它們。
上面的鏈接幫助我從interestList獲取動態創建的textViews的寬度。
通過在interestLinearLayout上使用ViewTreeObserver,我可以在布置LinearLayout后獲得它的寬度。
最后,應修改以下代碼,以在LinearLayout中從JAVA添加textViews。
final LinearLayout interestLinearLayout = findViewById(R.id.interests);
interestLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
interestLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
String interestList[]={"Travel","Music","Photography","Sports","Dance","Animals","SciFi Movies"};
int interestWidth =0;
int parentWidth = interestLinearLayout.getWidth(); // got width inside view tree observer for linearlayout
for(String interest: interestList) {
TextView textView = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(2,0,10,2);
textView.setLayoutParams(params);
textView.setPadding(2, 2, 2, 2);
textView.setText(interest);
textView.setIncludeFontPadding(true);
textView.measure(0,0); //using approach mentioned in link to get width of text views
interestWidth += textView.getMeasuredWidth();
if(interestWidth<parentWidth)
interestLinearLayout.addView(textView);
else
break;
}
}
});
要創建LinearLayout,
LinearLayout layout = new LinearLayout(MainActivity.this);
要設置布局的背景色,
layout.setBackgroundColor(Color.parseColor("#135517"));
要設置版式的寬度和高度,
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15, 5, 5, 5);
layout.setLayoutParams(params);
方向,
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
layout.setPadding(10, 10, 5, 5);
然后創建一個textview,
TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setPadding(2, 2, 2, 2);
textView.setText(" "your" ");
textView.setTextColor(getResources().getColor(R.color.white));
textView.setIncludeFontPadding(true);
將視圖添加到父級,
layout.addView(textView);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.