两个视图在彼此的顶部,但只显示一个?

时间:2016-04-26 16:40:32

标签: android xml imagebutton

我有一个ImageButton和一个在RelativeView内部具有相同大小宽度和高度的TextView。默认情况下,禁用TextView并启用ImageButton。但是,在这些情况下,我无法单击ImageButton。

当我启用TextView并禁用ImageButton时,我可以单击TextView。我认为这是因为它在XML中的ImageButton下。

有没有人知道如何才能获得这种“切换”功能,其中两个项目的尺寸相同,但一次只能点击一个?

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp">

                <ImageButton
                    android:id="@+id/pp_note_1"
                    android:layout_width="150dp"
                    android:layout_height="50dp"
                    android:layout_alignParentRight="true"
                    android:background="@android:color/white"/>

                <AutoCompleteTextView
                    android:enabled="false"
                    android:id="@+id/pp_autonote_1"
                    android:layout_alignParentRight="true"
                    android:layout_width="150dp"
                    android:layout_height="50dp" />


            </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

看看

public void setFocusable (boolean focusable)
public void setFocusableInTouchMode (boolean focusableInTouchMode)
public void setClickable (boolean clickable)

View

答案 1 :(得分:0)

这是使用ViewSwitcher切换两个视图的一个工作示例:

viewswitcher.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ViewSwitcher
    android:id="@+id/viewSwitcher"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="96dp">

    <ImageButton
        android:id="@+id/image"
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/text"
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</ViewSwitcher>

</RelativeLayout>

活动onCreate()

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

    final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
    final ImageButton imageButton = (ImageButton) findViewById(R.id.image);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewSwitcher.showNext();
        }
    });
}

要为过渡设置动画,请使用viewPager.setInAnimation()和.setOutAnimation()。

相关问题