2013-12-23 62 views
-1

我是Android編程的新手,我想在此Android應用程序中執行的操作是創建一個充滿按鈕的xml頁面。Android:無法防止按鈕增加大小並與其他按鈕重疊

當我點擊按鈕,該按鈕會變成淺綠色,當我再次點擊它,它會變成淺灰色

的錯誤:我得到的是,當我按一下按鈕,它增加在大小和與其他按鈕重疊,請幫我在這裏,它不是用戶在這種情況下

附在下面友好是代碼:

lockerbooking.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<Button 
    android:id="@+id/sisButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="16dp" 
    android:layout_marginTop="28dp" 
    android:text="@string/sis" 
    /> 

<Button 
    android:id="@+id/solButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/soeButton" 
    android:layout_alignBottom="@+id/soeButton" 
    android:layout_alignParentRight="true" 
    android:text="@string/sol" /> 

    <Button 
    android:id="@+id/soeButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/sisButton" 
    android:layout_alignBottom="@+id/sisButton" 
    android:layout_centerHorizontal="true" 
    android:text="@string/soe" /> 

    </RelativeLayout> 

代碼:

makeBooking.java

public class makeBooking extends Activity { 

Button sisButton; 
Button solButton; 
Button soeButton; 
Button sobButton; 



    super.onCreate(savedInstanceState); 
    // Get the message from the intent 
    setContentView(R.layout.lockerbookpage); 

    Intent intent = getIntent(); 
    // Initialize TextViews 
    sisButton = (Button) findViewById(R.id.sisButton); 
    solButton = (Button) findViewById(R.id.solButton); 
    soeButton = (Button) findViewById(R.id.soeButton); 
    sobButton = (Button) findViewById(R.id.sobButton); 
     } 
    public OnClickListener solButtonListener = new OnClickListener(){ 


    boolean flag = true; 

    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     if(flag){ 
      solButton.setBackgroundColor(Color.GREEN); 

     } 
     else{ 
      solButton.setBackgroundColor(Color.LTGRAY); 
     } 
     flag=!flag; 
    } 

}; 

...該代碼的推移

請幫我在這裏,我渴望學習

+1

您發佈的代碼不應該改變按鈕的大小。你是否也在改變按鈕中的文字? –

回答

0

避免按鈕重疊,使用固定寬度和高度按鈕: 更改爲:

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

對一些這樣的:

android:layout_width="100dp" //what ever size suits your layout 
android:layout_height="50dp" //fixing this,will not overlap the buttons