带有 ImageView 的按钮在 Table 包裹在线性布局中的布局内的相对布局中不可单击

Button with ImageView is not clickable in a Relative Layout inside a Table Layout wrapped in Linear Layout

我无法点击。我尝试使 Relative Layout 可点击且可聚焦,而 Button & ImageView 不可点击且不可聚焦,但它不起作用。我尝试在按钮和布局上设置点击事件。什么都不起作用。 以下是 main_activity.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.knit.cdc_knit.MainActivity"
    android:orientation="vertical">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5">
                    <RelativeLayout
                        android:id="@+id/knit_layout"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5"
                        >
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/aboutKnit"
                            android:background="@drawable/line_right_bottom_trans"
                            android:text="@string/aboutKnit"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"
                            />
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/college"
                            android:layout_centerInParent="true"
                            />
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/aboutCdc"
                            android:background="@drawable/line_bottom_trans"
                            android:text="@string/aboutCdc"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/depart"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5">
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/record"
                            android:background="@drawable/line_right_bottom_trans"
                            android:text="@string/record"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/record"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/company"
                            android:background="@drawable/line_bottom_trans"
                            android:text="@string/company"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/company"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5" >
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/training"
                            android:background="@drawable/line_right_bottom_trans"
                            android:text="@string/training"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/training"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/internship"
                            android:background="@drawable/line_bottom_trans"
                            android:text="@string/internship"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/intern"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5">
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/personality"
                            android:background="@drawable/line_right_trans"
                            android:text="@string/personality"
                            android:textSize="14sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/gallery"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/team"
                            android:background="@drawable/transparent"
                            android:text="@string/team"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/team"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
            </TableLayout>

</android.support.v7.widget.LinearLayoutCompat>

这是java代码:

    package com.knit.cdc_knit;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    Button btnKnit, btnCdc, btnRecord, btnCompany, btnTraining, btnintern, btnGallery, btnTeam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClickButtonListener(){
        btnKnit = findViewById(R.id.aboutKnit);
        RelativeLayout knitlayout = findViewById(R.id.knit_layout);
        knitlayout.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent iknit = new Intent(MainActivity.this, AboutKnit.class);
                        startActivity(iknit);
                    }
                }
        );
    }
}

这里是AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.knit.cdc_knit">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AboutKnit">
            <intent-filter>
                <action android:name="android.intent.action.AboutKnit" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".AboutCdc">
            <intent-filter>
                <action android:name="com.knit.cdc_knit.AboutCdc" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的布局如下: This is main_activity.xml

更好的方法是:

package com.knit.cdc_knit;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btnKnit, btnCdc, btnRecord, btnCompany, btnTraining, btnintern, btnGallery, btnTeam;
RelativeLayout knitlayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
onClickButtonListener();
    }
    public void onClickButtonListener(){
        btnKnit = findViewById(R.id.aboutKnit);
        knitlayout = findViewById(R.id.knit_layout);
        knitlayout.setOnClickListener(this);
btnKnit .setOnClickListener(this);

    }

 @Override
    public void onClick(View view) {
        if(view.getId() == R.id.knit_layout)
        {
 Intent iknit = new Intent(MainActivity.this, AboutKnit.class);
                        startActivity(iknit);
} else if(view.getId() == R.id.btnKnit )
        {
//Your Logic
}
}
In your activity call onClickButtonListener() method inside onCreate() 

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    // change
           onClickButtonListener();
    }

好吧,您正在为相关布局提供 ID,而您应该为 XML 中的按钮提供 ID 这是您的第一个按钮的示例。对所有按钮执行此操作

布局中的第一个按钮

<RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="aboutKnit"
                    android:id="@+id/about_Knit"
                    android:textSize="15sp"
                    android:gravity="center|bottom"
                    android:singleLine="false"
                    android:paddingBottom="10dp"
                    />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/college"
                    android:layout_centerInParent="true"
                    />
            </RelativeLayout>

主要活动

我已经为第一个按钮指定了 id 并为其设置了 onCLickListener。单击它会 o

public class MainActivity 扩展 AppCompatActivity {

Button btnKnit;

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

    btnKnit =(Button) findViewById(R.id.about_Knit);
    btnKnit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent iknit = new Intent(MainActivity.this, AboutKnit.class);
                    startActivity(iknit);
        }
    });
}

}

这肯定有用:)

工作,
稍微更改一下您的代码就可以了:
1) 调用 onClickButtonListener();在 onCreate 里面。
2) 将onlicklistner 从relativelayout 替换为button。

public class MainActivity extends AppCompatActivity {
Button btnKnit, btnCdc, btnRecord, btnCompany, btnTraining, btnintern, btnGallery, btnTeam;


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

    onClickButtonListener();
}

public void onClickButtonListener(){
    btnKnit = findViewById(R.id.aboutKnit);
    RelativeLayout knitlayout = findViewById(R.id.knit_layout);
    btnKnit.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent iknit = new Intent(MainActivity.this, AboutKnit.class);
                    startActivity(iknit);


                }
            }
    );
}
}