繁体   English   中英

动态在水平LinearLayout中添加TextViews

[英]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时尚未布置它们。

获取动态创建的textViews的宽度

上面的链接帮助我从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM