3

我的LinearLayout在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" /> 

,我想生成動態幾個另一LinearLayouts並把他們的「進步」 equaly間隔,例如:

  • 添加的第一個LinearLayout將佔據所有空間。
  • 二LL將分享1LL
  • 三LL的面積的50%將分享1LL空間的33%和2LL
  • 等等...

每個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

仔細檢查getNextRandomColor正在恢復的東西like.-

getResources().getColor(colorResId); 

而不只是一個colorResId。如果是這樣的話,你可以嘗試this.-

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

無論如何,如果你正在構建一個多色進度條,你應該考慮改變單一佈局的寬度,並使用漸變色。

+0

我getNextRandomColor方法: 公共靜態INT getNextRandomColor(){ INT [] =顏色新INT [] { R. color.color1, R.color.color2 }; 返回顏色[getNextRandomInt(0,colors.length)]; 我已經添加了「getResources()。getColor「,現在我有一些編譯錯誤。以前,使用這個方法,使用.xml LinearLayouts都可以很好地工作。 – Bresiu

+0

Thanks @Bresiu。我的猜測是正確的,'setBackgroundColor'不指望colorResId像R.color.turquoise ,但是可以通過'getResources()。getColor(R.color.turquoise)'獲得該顏色的真實值。只需嘗試返回'getResources()。getColor(colors [getNextRandomInt(0,colors.length)] );' – ssantos

+0

我編輯了我的第一條評論(不小心碰到輸入太快) – Bresiu

1

你可以試試這個。

activity_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> 

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

     } 
    } 


} 
+0

謝謝你提供的代碼,但我或多或少相同。這是getResourcess()的getColor()的問題(我有方法從R.color洗牌顏色) – Bresiu

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

OR

直接調用彩色

lp.setBackgroundColor(Color.WHITE); 
相關問題