Android 自定义线性布局中的按钮不可单击

Android 自定义线性布局中的按钮不可单击,android,Android,我有一个包含框架布局的自定义视图。此framelayout包含两个可以滑动的视图(LinearLayout)。如果我滑动第一个,则会出现第二个,反之亦然。其中一个视图有一个按钮,但我不知道为什么,这个按钮类似于禁用。我不能点击它,onClick方法没有效果 此处显示了自定义视图中的布局xml结构: <FrameLayout> <LinearLayout android:id="@+id/frontview" /> <LinearLayou

我有一个包含框架布局的自定义视图。此framelayout包含两个可以滑动的视图(LinearLayout)。如果我滑动第一个,则会出现第二个,反之亦然。其中一个视图有一个按钮,但我不知道为什么,这个按钮类似于禁用。我不能点击它,onClick方法没有效果

此处显示了自定义视图中的布局xml结构:

<FrameLayout>

  <LinearLayout
      android:id="@+id/frontview"
  /> 
  <LinearLayout
      android:id="@+id/backview">
    <Button
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:id="@+id/ButtonUpdate"
       android:text="@string/bUpdate"
       android:padding="5dp"
       android:clickable="true"
       style="?android:attr/borderlessButtonStyle"
        />
  </LinearLayout>

</FrameLayout>
使用
onInterceptTouchEvent(MotionEvent ev)
onTouchEvent(MotionEvent ev)
正确处理刷卡

下面是MyActivity中使用的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#ffffff">

   <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#f6f6f6">

      <TextView
         android:id="@+id/infoimc"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="18dp"
         android:layout_marginTop="8dp"
         android:text="@string/app_name"
         android:textColor="#000000"
         android:textSize="16dp"/>
      <View
         android:id="@+id/divider_infoimc"
         android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginRight="18dp"
            android:layout_marginLeft="18dp"
            android:background="#99CC00"/>
      <com.example.essai.CustomGraph
         android:id="@+id/CustomGraph"
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:layout_gravity="center"
         android:visibility="visible"
         android:layout_marginTop="10dp"/>

   </LinearLayout>

   <FrameLayout
      android:layout_width="wrap_content"
      android:layout_height="80dp"
      android:layout_gravity="left|center_vertical">

   <com.example.essai.mView
      android:id="@+id/CustomView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

   </FrameLayout>

</LinearLayout>
我不知道按钮是否也必须在活动中处理

谢谢你的帮助

根本原因 根据:

FrameLayout设计用于在屏幕上标出要显示的区域 单个项目

如果有更多项目,如您的情况,则会发生“意外”情况:

子视图绘制在堆栈中,其中包含最近添加的子视图 在上面

这意味着,您的
frontview
位于
backview
之上,并且由于
frontview
没有
android:clickable=“true”
,因此单击事件(on按钮)不在下方委派

解决方案1 以编程方式重新排序子布局。

但是,可以向FrameLayout和控件添加多个子项 通过将重力指定给每个框架布局,它们在框架布局中的位置 使用android:layout\u gravity属性创建子对象

只要在滑动时切换android:layout\u gravity=“top”和
android:layout\u gravity=“bottom”

解决方案2 以编程方式控制子布局的可见性。

当应显示
后视图
时,将
前视图
的可见性设置为
View.GONE
。并将其设置为查看。在相反的情况下可见

解决方案3
框架布局更改为不同的布局类型。

可能需要更多的“摆弄”布局XML


看看你最熟悉的是什么,然后相应地选择解决方案:)

谢谢你的回答。我尝试了第一种解决方案,通过编程方式在
init()
中添加了
android:layout\u gravity=“top
android:layout\u gravity=“bottom”
,但没有效果。因此,我将后视图放在框架布局的顶部,并将前视图的可见性设置为“消失”,但结果类似,按钮为禁用:(实际上,我想要Gmail中的类似内容。在滑动第一个视图后,第二个视图可用于执行某些操作。在我的情况下,我有一个EditText和一个按钮。尝试了几次使用第一个解决方案后(未成功),我决定尝试第二种解决方案,最后我认为问题在于我处理滑动的方式。这就是我想要建议的——将代码张贴在滑动视图的位置。如果解决方案对您有效,通常的过程是接受它:)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#ffffff">

   <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#f6f6f6">

      <TextView
         android:id="@+id/infoimc"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="18dp"
         android:layout_marginTop="8dp"
         android:text="@string/app_name"
         android:textColor="#000000"
         android:textSize="16dp"/>
      <View
         android:id="@+id/divider_infoimc"
         android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginRight="18dp"
            android:layout_marginLeft="18dp"
            android:background="#99CC00"/>
      <com.example.essai.CustomGraph
         android:id="@+id/CustomGraph"
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:layout_gravity="center"
         android:visibility="visible"
         android:layout_marginTop="10dp"/>

   </LinearLayout>

   <FrameLayout
      android:layout_width="wrap_content"
      android:layout_height="80dp"
      android:layout_gravity="left|center_vertical">

   <com.example.essai.mView
      android:id="@+id/CustomView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

   </FrameLayout>

</LinearLayout>
public class MyActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   }

}