将两个图像按钮放在一起

时间:2014-02-03 23:23:09

标签: android android-layout

我需要将两个ImageButton放在彼此之上,如下图所示。 enter image description here

虽然放置它们没有问题,但我无法点击蓝色按钮。点击时的红色按钮效果非常好。

XML布局代码如下:

        <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageButton
            android:id="@+id/bluebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:onClick="onButtonClicked"
            android:scaleType="fitCenter"
            android:src="@drawable/bluebutton" />

        <ImageButton
            android:id="@+id/redbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:onClick="onButtonClicked"
            android:scaleType="fitCenter"
            android:src="@drawable/redbutton" />
    </FrameLayout>

如何确保可以点击两个按钮?

2 个答案:

答案 0 :(得分:1)

您只需使用RelativeLayout即可。看到我刚刚做的这个:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton btn = (ImageButton)findViewById(R.id.bluebutton);
    ImageButton btn2 = (ImageButton)findViewById(R.id.redbutton);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView t = (TextView)findViewById(R.id.txt);
            t.setText("Hello!");
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView t = (TextView)findViewById(R.id.txt);
            t.setText("Hi, how are you?");
        }
    });
}

这只是一个例子

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageButton
    android:background="#ff040ab2"
    android:id="@+id/bluebutton"
    android:layout_width="150dp"
    android:layout_height="250dp"
    android:onClick="onButtonClicked"
    android:scaleType="fitCenter"/>

<ImageButton
    android:rotation="90"
    android:background="#be2222"
    android:id="@+id/redbutton"
    android:layout_width="150dp"
    android:layout_height="250dp"
    android:onClick="onButtonClicked"
    android:scaleType="fitCenter"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txt"
    android:textColor="#ffffff"/>
</RelativeLayout>

enter image description here enter image description here

答案 1 :(得分:1)

更改了代码。

  public class MainActivity extends Activity {

        ImageButton red,blue;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            red = (ImageButton)findViewById(R.id.redbutton);

            blue = (ImageButton)findViewById(R.id.bluebutton);

        }

public void onRedButtonClicked(final View view) {
        Toast.makeText(getApplicationContext(), "Button red is clicked!", Toast.LENGTH_SHORT).show();
    }
    public void onBlueButtonClicked(final View view) {
        Toast.makeText(getApplicationContext(), "Button blue is clicked!", Toast.LENGTH_SHORT).show();
    }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

布局:

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/redbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:onClick="onRedButtonClicked"
        android:scaleType="fitCenter"
        android:src="@drawable/redbutton" 
        android:visibility="visible"/>

    <ImageButton
        android:id="@+id/bluebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:onClick="onBlueButtonClicked"
        android:scaleType="fitCenter"
        android:src="@drawable/bluebutton" 
        android:visibility="visible"/>

</FrameLayout>

希望这会有所帮助.. :)