简体   繁体   English

在水平的线性布局中均匀分布文本视图并包装内容

[英]Evenly distribute textviews across linear layout horizontally and wrap content

I want to programmatically add some textviews inside a linear layout. 我想以编程方式在线性布局内添加一些textview。 I want these textviews to evenly distribute across the width of the linear layout and i want the textviews to wrap their content. 我希望这些textview在线性布局的宽度上均匀分布,并且我希望textview包装它们的内容。

I create the textviews like that, but they dont wrap their content. 我这样创建textview,但是它们不包装其内容。

    LinearLayout linearLayout = new LinearLayout(mContext);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.BOTTOM;
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);

    params.setMargins(40, 0, 0, 0);
    linearLayout.setLayoutParams(params);
   // linearLayout.setGravity(Gravity.BOTTOM);

    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    for(String label: labels) {
        TextView textView = new TextView(mContext);
        textView.setText(label.trim());
        textView.setTextSize(13);
        textView.setBackgroundColor(Color.GREEN);
        textView.setTextColor(R.color.labels_color);
        LinearLayout.LayoutParams mlp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
        mlp.setMargins(0,0,20,0);

        textView.setLayoutParams(mlp);
        linearLayout.addView(textView);
    }
    chartWrapper.addView(linearLayout);

You should use weightsum and weight with LinearLayout for distributing items evenly and you are using only weight as far as i can see. 您应该将weightsum和weight与LinearLayout一起使用以平均分配项目,并且据我所知,您仅使用weight。 Without a proper weightsum, the linearlayout will not know how to distribute. 没有合适的权重,线性布局将不知道如何分配。

If that doesnt work, try setting the width to 0, I think its a rquirement for weight to work properly. 如果这样不起作用,请尝试将宽度设置为0,我认为这是重量才能正常工作的要求。 But you want your textviews to have wrap_content so maybe you should put the textview in a layout and give the layout 0 width so you can make your textview wrap_content. 但是,您希望文本视图具有wrap_content,因此也许应该将文本视图放置在布局中,并给布局提供0宽度,以便可以使textview wrap_content成为可能。

Try instead of 尝试代替

LinearLayout.LayoutParams mlp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);

Use zero as width: 使用零作为宽度:

LinearLayout.LayoutParams mlp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);

Hope it helps 希望能帮助到你

I want these textviews to evenly distribute across the width of the linear layout and i want the textviews to wrap their content 我希望这些textview在线性布局的宽度上均匀分布,并且我希望textview包装其内容

You're aiming for two contradicting designs, either the width of the TextView is according to its content (WRAP_CONTENT) or it's the width of the LinearLayout divided by the number of TextViews inside it. 您的目标是两个矛盾的设计,要么TextView的宽度根据其内容(WRAP_CONTENT)要么是LinearLayout的宽度除以其中的TextViews数量。

If you want a TextView to wrap its content then define it in the LayoutParams just as you did. 如果您希望TextView包装其内容,请像以前一样在LayoutParams中对其进行定义。 If you want to divide the width of the LinearLayout evenly then set the gravity of the TextView to 1 and its width to 0. 如果要均匀地划分LinearLayout的宽度,则将TextView的重力设置为1,并将其宽度设置为0。

BTW, textView.setTextSize(13); 顺便说一句, textView.setTextSize(13); sets the size in pixels and is not recommended, you should use dp or sp instead. 以像素为单位设置大小,不建议您使用dp或sp。

It's not a good idea that you add margin for each items. 为每个项目添加边距不是一个好主意。 It works with small number of items, but if you are adding like 10 items, those margins will push contents too much. 它只适用于少量项目,但是如果您要添加10个项目,那么这些边距将过多地推动内容。

I created a project that handles textview dynamically (landscape and portrait) with using LinearLayout and ConstraintLayout https://github.com/SwordArtist/DynamicEvenSpreadingView 我创建了一个使用LinearLayout和ConstraintLayout动态处理textview(横向和纵向)的项目https://github.com/SwordArtist/DynamicEvenSpreadingView

Hope it helps 希望能帮助到你

Fixed it by assigning weight to 1 to all TextViews except the last one. 通过将除最后一个之外的所有TextView的权重分配为1来解决此问题。

    public void drawLabels(List<String> labelsValues, ViewGroup container) {


    for (int i = 0; i < labelsValues.size(); i++) {
        String label = labelsValues.get(i);
        TextView textView = new TextView(mContext);
        textView.setText(label.trim());

        if (i == labelsValues.size() - 1) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(params);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
            textView.setLayoutParams(params);
        }
        container.addView(textView);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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