簡體   English   中英

在線性布局中無法單擊按鈕

[英]Can't Click Button whilst in linear layout

我希望我的兩個按鈕彼此內嵌並占用相同的空間,所以我認為最好使用 LinearLayout。 但是,當我將它們放在線性布局中時,我無法再單擊按鈕。

<LinearLayout
    android:id="@+id/linearLayoutStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="30dp"
    android:clickable="true"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_alignParentStart="true">

    <Button
        android:id="@+id/btn_signup"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:background="@drawable/input_frame_white"
        android:text="@string/skip"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:background="@drawable/input_frame_white"
        android:text="@string/next"
        android:textColor="@android:color/white" />
</LinearLayout> 

這是java調用:

================================================ ======================

    btnSignUp = (Button) findViewById(R.id.btn_signup);
    btnLogIn = (Button) findViewById(R.id.btn_login);

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            launchSignUp();
        }
    });

    btnLogIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            launchLogInScreen();
        }
    });

謝謝你們,非常感謝。

如果在示例代碼之后還有另一個布局元素,它可能會呈現在按鈕之上。 將包含按鈕的線性布局(如上面的代碼示例中所示)作為最后一個元素。 例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayoutStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="30dp"
        android:clickable="true"
        android:weightSum="2"
        android:orientation="horizontal"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/btn_signup"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1"
            android:background="@drawable/input_frame_white"
            android:text="@string/skip"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1"
            android:background="@drawable/input_frame_white"
            android:text="@string/next"
            android:textColor="@android:color/white" />
    </LinearLayout> 

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我認為是因為這段代碼:

機器人:點擊=“真”

在你的 LinearLayout

刪除此代碼並重試。

代碼是正確的。 我唯一能想到的就是

android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"

顯示您的 LinearLayout 位於 RelativeLayout 內,並且 RelativeLayout 的某些透明元素可能與按鈕重疊。

這如果 - 正如你所說 - 從 LinearLayout 中刪除“可點擊”並沒有解決問題(實際上:如果 LinearLayout 是可點擊的,按鈕可能不會收到點擊)

查看按鈕代碼之前,可能有異常(錯誤)讓應用程序直接跳轉捕獲,logcat 可以幫助您查看那些異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM