简体   繁体   English

Android studio 上的 Android 仿真器中未显示按钮

[英]Buttons not showing in Android emulator on Android studio

I am trying to make a like a user button navigation for my project by having buttons however whenever I run the android emulator my design of the buttons I have added isn't showing at all which is confusing as I don't know why it isn't showing at all, I have had a bit of experience with the android studio but I have never had this issue before where the screen is just blank.我试图通过按钮为我的项目制作一个类似用户按钮导航但是每当我运行 android 仿真器时,我添加的按钮设计根本没有显示,这令人困惑,因为我不知道为什么会这样' 根本没有显示,我对 android 工作室有一些经验,但在屏幕空白之前我从未遇到过这个问题。 Here are the screenshots of my design and then also the design when I run the emulator.这是我的设计截图,还有我运行模拟器时的设计截图。 设计 安卓模拟器空白?

Here is the text of the design这是设计的文字

<?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"
tools:context=".MainActivity">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="101dp"
    android:layout_marginLeft="101dp"
    android:layout_marginTop="213dp"
    android:layout_marginEnd="123dp"
    android:layout_marginRight="123dp"
    android:layout_marginBottom="470dp"
    android:text="@string/identify_the_car_make" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="147dp"
    android:layout_marginLeft="147dp"
    android:layout_marginTop="277dp"
    android:layout_marginEnd="176dp"
    android:layout_marginRight="176dp"
    android:layout_marginBottom="406dp"
    android:text="@string/hints" />

<Button
    android:id="@+id/button2"
    android:layout_width="204dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="89dp"
    android:layout_marginLeft="89dp"
    android:layout_marginTop="326dp"
    android:layout_marginEnd="118dp"
    android:layout_marginRight="118dp"
    android:layout_marginBottom="357dp"
    android:text="@string/identify_the_car_image" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="121dp"
    android:layout_marginLeft="121dp"
    android:layout_marginTop="376dp"
    android:layout_marginEnd="149dp"
    android:layout_marginRight="149dp"
    android:layout_marginBottom="307dp"
    android:text="@string/advanced_level" />
</RelativeLayout>

This can be happen because of:这可能是因为:

  1. The button is off screen as @fjmarquez said.正如@fjmarquez 所说,该按钮不在屏幕上。 Because it staticly margin the value no matter what device your application will be installed.因为无论您的应用程序将安装什么设备,它都会静态地保留该值。 Margin 100dp for tablet will be looks smaller than phone, even phone with 5 inch screen will difference with another inch device.平板的margin 100dp 看起来会比手机小,即使是5 英寸屏幕的手机也会与其他英寸的设备有所不同。

  2. You are forgot to add setContentView() .您忘记添加setContentView()

I suggest to use ConstraintLayout for complex view, and use LinearLayour if your page quiet simple.我建议对复杂视图使用ConstraintLayout ,如果您的页面很简单,请使用LinearLayour For you case, you can use LinearLayour with center gravity instead.对于您的情况,您可以改用具有中心重心的LinearLayour

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:layout_width="204dp"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

The elements are probably off screen, this can be caused by margins.元素可能不在屏幕上,这可能是由边距引起的。

Try not to use margin start and margin left or margin end and margin right at the same time.尽量不要同时使用margin start 和margin left 或margin end 和margin right。 Take a look at this to understand.看看这个就明白了。

I recommend using constraint layout.我建议使用约束布局。

I suggest using constraint layout because relative layout is deprecated.我建议使用约束布局,因为不推荐使用相对布局。

read the documentation about it at the following link在以下链接中阅读有关它的文档

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

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