代码之家  ›  专栏  ›  技术社区  ›  Shivam Tiwari

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

  •  0
  • Shivam Tiwari  · 技术社区  · 8 年前

    我无法单击。我试着使相对布局可点击、可聚焦,以及按钮&ImageView不可单击且不可聚焦,但它不工作。我尝试在按钮和布局上设置click事件。什么都没用。 以下是主要的\u活动。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 回复  |  直到 8 年前
        1
  •  1
  •   matin sayyad    8 年前

    更好的方法是:

    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
    }
    }
    
        2
  •  1
  •   Rajasekhar    8 年前

    好的,您为相对布局提供了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

    公共类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
  •  1
  •   anil    8 年前
    In your activity call onClickButtonListener() method inside onCreate() 
    
    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        // change
               onClickButtonListener();
        }
    
        4
  •  1
  •   Amit Sharma    8 年前

    工作 ,则,
    您的代码稍作更改即可:
    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);
    
    
                    }
                }
        );
    }
    }