动态在水平LinearLayout中添加TextViews

时间:2019-01-25 00:37:55

标签: android android-layout android-linearlayout textview

Click here to see the image

在我的应用程序的配置文件页面中,我想要一个感兴趣的部分,如图所示。用户在其个人资料下有一个兴趣列表。我想在水平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;
    }

4 个答案:

答案 0 :(得分:0)

您可以动态添加视图,但首先需要引用要向其添加视图的父视图。

您可以只使用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文件一起发布,以便我更好地回答您的问题。

答案 1 :(得分:0)

要创建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 = 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);

答案 2 :(得分:0)

在执行到达该行时尚未创建视图

if(interestWidth<parentWidth)

因此,大小0将存储在动态创建的textView的parentWidth和interestWidth中,以进行关注。

请查看有关Android文档中的视图生命周期的信息。

答案 3 :(得分:0)

interestWidth和parentWidth最初为0,因为调用getWidth时它们尚未布置。

https://www.php.net/manual/en/function.getopt.php

上面的链接帮助我从interestList获得动态创建的textView的宽度。

通过在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;
                }
            }
        });
相关问题