如何通过单击FrameLayout打开活动?

时间:2016-03-29 02:02:53

标签: android android-layout

事实上,我知道如何通过点击按钮来打开活动

但我不能在框架布局中做到这一点

我尝试使用此代码,但它并没有帮助我。

 FrameLayout ff=(FrameLayout)findViewById(R.id.btn);
    ff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(listActivity.this,bookActivity.class);
            startActivity(intent);
        }
    });

全部

EDITES:

这是我的activity_main:

 <LinearLayout
                android:id="@+id/linbook"
                android:layout_margin="15dp"
                android:layout_toRightOf="@+id/linjozve"
                android:layout_width="130dp"
                android:layout_height="180dp"
                android:orientation="vertical">
                <FrameLayout

                    android:id="@+id/ketabbtn"
                    android:layout_width="130dp"
                    android:layout_height="130dp">

                    <ImageButton
                        android:layout_weight="1"
                        android:id="@+id/btntopright"
                        android:layout_width="130dp"
                        android:layout_height="130dp"
                        android:background="@drawable/rgreen"/>

                    <ImageButton
                        android:background="@drawable/book"
                        android:layout_gravity="center"
                        android:layout_width="80dp"
                        android:layout_height="80dp" />

                </FrameLayout>
                <TextView
                    android:layout_marginTop="15dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="book"/>
        </LinearLayout>

感谢

2 个答案:

答案 0 :(得分:0)

问题是,如果FrameLayout被其他视图覆盖,则很难点击它。在您的情况下,framelayoutimagebutton具有相同的尺寸130x130。这解释了为什么触摸无法达到布局。

要确保可以单击它,请在布局及其子项之间添加一些填充。 (我缩小了孩子的体型)

<FrameLayout
                android:padding="10dp"
                android:id="@+id/ketabbtn"
                android:layout_width="130dp"
                android:layout_height="130dp">

                <ImageButton
                    android:layout_weight="1"
                    android:id="@+id/btntopright"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:background="@drawable/rgreen"/>

                <ImageButton
                    android:background="@drawable/book"
                    android:layout_gravity="center"
                    android:layout_width="80dp"
                    android:layout_height="80dp" />

</FrameLayout>

另外,您使用了错误ID 。所以改变这个:

FrameLayout ff=(FrameLayout)findViewById(R.id.btn);

对此:

FrameLayout ff=(FrameLayout)findViewById(R.id.ketabbtn);

因此,您可以单击填充空间以触发onClick事件。

答案 1 :(得分:0)

尝试在xml中设置android:clickable。 如果这不起作用,请尝试创建自定义布局:  默认情况下,onInterceptTouchEvent返回false。因此,请包含以下代码并使用此CustomClickableFrameLayout而不是framelayout

public class CustomClickableFrameLayout extends FrameLayout {
    private OnClickListener mOnClickListener;

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(l);
        mOnClickListener = l;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mOnClickListener != null;
    }


    // Standard constructors — just pass everything
    public CustomClickableFrameLayout (final Context context) {
        super(context);
    }

    public CustomClickableFrameLayout (final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomClickableFrameLayout (final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomClickableFrameLayout (final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}