如何以编程方式向另一个布局添加具有背景颜色和权重的LinearLayout?

12

我有一个在xml中的LinearLayout:

    <LinearLayout
        android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/progress_height"
        android:layout_alignParentBottom="true"
        android:baselineAligned="false"
        android:orientation="horizontal" />

我想动态生成几个LinearLayout,并将它们等间距地放置在"progress"中,例如:

  • 第一个LinearLayout将占据所有空间。
  • 第二个LinearLayout将与第一个LinearLayout共享50%的空间
  • 第三个LinearLayout将与第一个和第二个LinearLayout共享33%的空间
  • 以此类推...

每个LinearLayout都将具有随机背景颜色。 我写了类似这样的代码:

mProgress = (LinearLayout) findViewById(R.id.progress);
.
.
.
LinearLayout prog = new LinearLayout(this);
            prog.setBackgroundColor(CommonUtils.getNextRandomColor());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);

            prog.setLayoutParams(params);
            mProgress.addView(prog);
当用户按下按钮时,将生成另一个带有不同颜色的LL。
我的方法不起作用。布局中没有背景颜色。
也许有另一种更简单的方法来实现进度条,使颜色在相等的空间中共享?
3个回答

14

请确认 getNextRandomColor 返回的内容类似于以下示例.-

getResources().getColor(colorResId);

而不仅仅是 colorResId。如果是这样的话,您可以尝试这个。

prog.setBackgroundColor(getResources().getColor(CommonUtils.getNextRandomColor()));

无论如何,如果您正在构建一个多颜色进度条,您应该考虑更改单个布局的宽度,并使用渐变颜色。


谢谢 @Bresiu。那我的猜测就是对的。setBackgroundColor 不像 R.color.turquoise 期望一个 colorResId,而是这个颜色的实际值,你可以通过 getResources().getColor(R.color.turquoise) 获取它。试着返回 getResources().getColor(colors[getNextRandomInt(0, colors.length)]); - ssantos
我已经编辑了我的第一条评论(不小心按下了回车键)。 - Bresiu
好的,你在getResources()中缺少括号。 - ssantos
1
好的,我的错,那么最好还是保留你原来的 getNextRandomColor 方法,并从 Activity 中调用 getResources().getColor()。我正在编辑我的答案。 - ssantos
好的,现在它可以工作了:return context.getResources().getColor(colors[getNextRandomInt(0, colors.length)]); 谢谢你,我会将你的回答标记为正确。而且我认为我可以尝试一下你的渐变想法 - 它更加优雅。 - Bresiu
显示剩余3条评论

5
LinearLayout lp = new LinearLayout(context) ;
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height , .60f);
lp.setLayoutParams(layoutParams);


//for setting the background color  // input your color
LinearLayout.setBackgroundColor(Color.parseColor("#000000"));

或者

直接调用颜色

lp.setBackgroundColor(Color.WHITE);

2
你可以尝试这个。
活动主界面.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Layout" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:id="@+id/main_lay">
    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.testlayout;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity implements OnClickListener{


    private Button add_btn;
    private  LinearLayout main_lay;
    private LinearLayout.LayoutParams param;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init()
    {
        main_lay = (LinearLayout)findViewById(R.id.main_lay);
        add_btn = (Button)findViewById(R.id.button1);
        add_btn.setOnClickListener(this);
        param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,1);

    }

    @Override
    public void onClick(View v) {

        if(v == add_btn)
        {
            LinearLayout lay = new LinearLayout(this);
            lay.setLayoutParams(param);
            Random rnd = new Random(); 
            int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
            lay.setBackgroundColor(color);
            main_lay.addView(lay);

        }
    }


}

谢谢您提供代码,但我的代码与您的差不多。问题出在getResources().getColor()上(我有一个从R.color中随机选择颜色的方法)。 - Bresiu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接