【问题标题】:Placing Two Image Buttons on Top of Each Other将两个图像按钮放在一起
【发布时间】:2014-02-04 09:41:43
【问题描述】:

我需要将两个 ImageButton 叠放在一起,如下图所示。

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

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>

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

【问题讨论】:

    标签: android android-layout


    【解决方案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>
    

    【讨论】:

    • 此 XML 布局是较大布局的一部分,其中有更多按钮.. 所以我不能像您所做的那样为单个按钮设置 ClickListener。它们只需要在 XML 中设置。
    【解决方案2】:

    修改了代码。

      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>
    

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

    【讨论】:

    • 此 XML 布局是较大布局的一部分,其中有更多按钮.. 所以我不能像您所做的那样为单个按钮设置 ClickListener。它们只需要在 XML 中设置。
    • 两个图像按钮上都有 android:onClick="onButtonClicked" 。也许您可以尝试将其更改为不同的东西。
    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多