Android 如何通过编程将带有背景色和权重的线性布局添加到另一个布局

Android 如何通过编程将带有背景色和权重的线性布局添加到另一个布局,android,android-layout,android-linearlayout,layoutparams,Android,Android Layout,Android Linearlayout,Layoutparams,我有xml中的LinearLayout: <LinearLayout android:id="@+id/progress" android:layout_width="fill_parent" android:layout_height="@dimen/progress_height" android:layout_alignParentBottom="true" android:baselineAli

我有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" />
当用户按下按钮时,将生成另一个不同颜色的LL

我的方法行不通。布局中没有背景色

也许还有另一种更简单的方法来实现某种进度条,颜色平均共享一些空间?

你可以试试这个

活动\u main.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>

再次检查
getNextRandomColor
是否返回类似的内容-

getResources().getColor(colorResId);
而不仅仅是一个
colorResId
。如果是这样的话,你可以试试这个-

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

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

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);

我的getNextRandomColor方法:公共静态int getNextRandomColor(){int[]colors=new int[]{R.color.color1,R.color.color2};返回颜色[getNextRandomInt(0,colors.length)];我添加了“getResources().getColor”现在我有一些编译错误。以前,使用这种方法,使用.xml LinearLayouts,一切都很好。谢谢@Bresiu。我当时的猜测是正确的。
setBackgroundColor
不期望像R.color.turquoise这样的colorrist,但该颜色的真实值可以通过
getResources().getColor(R.color.turquoise)获得
。只需尝试返回
getResources().getColor(colors[getNextRandomInt(0,colors.length)];
我已经编辑了我的第一条评论(意外地太快点击回车键)Gradle:error:找不到符号变量getResources with:return getResources.getColor(colors[getNextRandomInt(0,colors.length)]);是的,我的错,离开你原来的
getNextRandomColor
,从
活动中调用
getResources().getColor()
会更容易些。我正在编辑我的答案。谢谢你提供代码,但我的代码大致相同。这是getResourcess().getColor()问题(我有一种从R.color中洗牌颜色的方法)
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);