Android:无法防止按钮增加大小并与其他按钮重叠



我是Android编程的新手,我在这个Android应用程序中尝试做的是创建一个充满按钮的xml页面。

当我单击按钮时,

按钮将变为浅绿色,当我再次单击它时,它将变为浅灰色

错误:我得到的是当我单击按钮时,它的大小会增加并与其他按钮重叠,请在这里帮助我,在这种情况下它不是用户友好的

下面附上代码:

储物柜预订.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>

法典:

预订.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;
    }
};

。代码继续

请在这里帮助我,我渴望学习

为避免按钮重叠,请对按钮使用固定宽度和高度:更改此内容:

 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

最新更新