带有ImageView的按钮在“线性布局”中包含的“表格布局”内的“相对布局”中无法单击

时间:2018-03-15 06:10:03

标签: android android-linearlayout android-imageview android-tablelayout android-relativelayout

我无法点击。我尝试使相对布局可点击并且可聚焦,而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

4 个答案:

答案 0 :(得分:0)

更好的解决方法是:

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
}
}

答案 1 :(得分:0)

In your activity call onClickButtonListener() method inside onCreate() 

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

答案 2 :(得分:0)

好吧,你给了相对布局的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>

MainActivity

我已将id设为第一个按钮并将onCLickListener设置为它。单击它将

公共类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);
        }
    });
}

}

这肯定有效:)

答案 3 :(得分:0)

<强>工作下,
你的代码有点变化,没关系:
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);


                }
            }
    );
}
}