简体   繁体   English

ImageButton不出现在布局上

[英]ImageButtons not appear on the layout

I have 2 layouts: activity_main and activity_car_service ; 我有2种布局: activity_mainactivity_car_service I created some mipmaps from where I got my images buttons source 我从获取图像按钮源的位置创建了一些mipmaps

When running the app the activity_main contains all my buttons but when pressing an imageButton to open the activity_car_service can not see any of them... 运行该应用程序时, activity_main包含我所有的按钮,但是按imageButton打开activity_car_service时,看不到任何按钮...

activity_car_service is declared in manifest, still not see any imageButton activity_car_service在清单中声明,但仍然看不到任何imageButton

Here is activity_car_service 这是activity_car_service

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_height="wrap_content">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Button" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:clickable="true"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/imageButton1111"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                android:background="@null"
                android:onClick="sendMessage"
                android:scaleType="fitCenter"
                app:srcCompat="@mipmap/ic_auto_tracting" />

            <ImageButton
                android:id="@+id/imageButton2111"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_auto_lighting" />

            <ImageButton
                android:id="@+id/imageButton3111"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:adjustViewBounds="true"
                android:background="@null"
                android:scaleType="fitCenter"
                app:srcCompat="@mipmap/ic_carservice" />

            <ImageButton
                android:id="@+id/imageButton34111"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_auto_oil" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:clickable="true"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_auto_brakes" />

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_auto_battery" />

            <ImageButton
                android:id="@+id/imageButton6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_auto_engine" />

            <ImageButton
                android:id="@+id/imageButton66"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_auto_brakes" />

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

Here is how I call it: 这是我的称呼:

public class CarServiceSubMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_service);
    }
}

Manifest: 表现:

<activity android:name="utils.CarServiceSubMenu">
    android:label = "lala"/>
</activity>

MainActivity: 主要活动:

public void openCarServiceSubMenu(View view)
{
    Intent intent = new Intent(MainActivity.this, CarServiceSubMenu.class);
    startActivity(intent);
}

activity_main which is working: activity_main正在运行:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sharmind93.nj00.MainActivity">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:clickable="true"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:onClick="sendMessage"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_fire" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_plumbing" />

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:onClick="openCarServiceSubMenu"
                app:srcCompat="@mipmap/ic_carservice" />

            <ImageButton
                android:id="@+id/imageButton34"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_dentist" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:clickable="true"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_lightning" />

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_hairdress" />

            <ImageButton
                android:id="@+id/imageButton6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_carwash" />

            <ImageButton
                android:id="@+id/imageButton66"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.00"
                android:background="@null"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                app:srcCompat="@mipmap/ic_misc" />

        </LinearLayout>

    </RelativeLayout>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_dialog_info"
        android:id="@+id/imageButton16"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="false"
        android:layout_centerInParent="false"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="false" />
</RelativeLayout>

What can be the problem ? 可能是什么问题? I spent so much time to try out different things ... 我花了很多时间尝试不同的东西...

Please help! 请帮忙!

Your ImageButtons have width=match_parent while its parent have width=wrap_content. 您的ImageButton具有width = match_parent,而其父级具有width = wrap_content。

Their size is 0. 它们的大小为0。

Change the first LLayout with to match_parent too so you can see then. 将第一个LLayout也更改为match_parent,这样您就可以看到。

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"

Also,your layout will cover the button, try to use below=button (since you are using RelativeLayout as parent and height=wrap_content 另外,您的布局将覆盖按钮,请尝试使用below = button(因为您将RelativeLayout用作父对象,而height = wrap_content

The RelativeLayout can be used for adding constraint like below/onTopof/toRightof other views in the layout, you are overlaping your layout with other views and cant see a thing. RelativeLayout可用于在布局中添加约束,例如在其他视图的below / onTopof / toRightof中,您将布局与其他视图重叠,看不到东西。

实际上问题是我使用了app:srcCompat,但是我的活动不是AppCompatActivity...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM