2014-04-06 92 views
4

如何使單個按鈕寬度以編程方式填充父項?我已經做到了這一點,但它似乎無法工作,它仍然是位於左上角與寬度僅僅包裹內容...這裏是一些按鈕創建的代碼...使單個按鈕寬度以編程方式填充父項

public class TEST_GIFDisplay extends Activity implements View.OnClickListener { 

    SurfaceView sview; 
    GifRun gr = new GifRun(); 
    Button btnBack; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     sview = new SurfaceView(this); 
     sview.setZOrderOnTop(true); 

     gr.LoadGiff(sview, this, R.drawable.smiley); 

     LinearLayout linearLayout1 = new LinearLayout(this); 
     linearLayout1.setOrientation(LinearLayout.VERTICAL); 
     linearLayout1.setBackgroundColor(Color.parseColor("#27ae60")); 
     linearLayout1.setLayoutParams(new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT)); 

     LinearLayout linearLayout2 = new LinearLayout(this); 
     linearLayout2.setOrientation(LinearLayout.VERTICAL); 
     linearLayout2.setBackgroundColor(Color.parseColor("#27ae60")); 
     linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT)); 

     LinearLayout.LayoutParams p = new 
     LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT); 
     p.weight = 1.0f; 

     btnBack = new Button (this); 
     btnBack.setText("Back"); 
     btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); 
     btnBack.setGravity(Gravity.CENTER_HORIZONTAL); 
     btnBack.setLayoutParams(p); 
     btnBack.setOnClickListener(this); 

     linearLayout2.addView(btnBack); 
     linearLayout1.addView(linearLayout2); 
     linearLayout1.addView(sview); 

     setContentView(linearLayout1); 
    } 

    @Override 
    public void onClick(View view) { 
     btnBack = (Button) view; 
     btnBack.setBackgroundColor(Color.parseColor("#2980b9")); //color change 
     new CountDownTimer(100, 50) { 

      @Override 
      public void onTick(long arg0) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void onFinish() { 
       btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); // original color 
      } 
     }.start();//end color changed 

     finish(); 
    } 
} 
+0

你在哪裏添加這個按鈕到你的佈局,請張貼代碼了。 –

+0

@MrSMAK我用完整的代碼編輯了代碼,我想創建一個活動,顯示一個gif圖像並能夠返回到前一個屏幕,所以按鈕將轉到前一個屏幕 – Chrisantics

回答

3

您要添加的ButtonlinearLayout2。您應該linearLayout2's寬度更改爲MATCH_PARENT

linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
     ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT)); 

我希望這有助於。

P.S:您可以創建Button's選擇器的按下和選定狀態,而不是使用計時器顯示按下的按鈕效果。這裏有一個基本的鏈接,可以幫助你實現這一點:android button selector

+0

非常感謝@MrSMAK!有效!!順便說一句,對於你已經共享的android選擇器鏈接,如果我的按鈕是以編程方式創建而不是在xml佈局中創建的,如何將按鈕背景設置爲xml選擇器文件?先謝謝了! – Chrisantics

+1

我的榮幸:-),您可以將選擇器添加到'drawable'目錄並將其設置爲您的按鈕,如下所示:'Button b = new Button(Context); b.setBackgroundResource(R.drawable.YOUR_XML_SELECTOR);' –

+0

yeap再次感謝您對按鈕顏色變化的回答!它工作,但不僅僅是按鈕,整個屏幕設置爲相同的按鈕效果。爲什麼是這樣?對不起,我是一個新手! – Chrisantics

相關問題