【问题标题】:Android - Button does not appearAndroid - 按钮不出现
【发布时间】:2017-08-05 13:16:29
【问题描述】:

我是 Android 开发的新手,我正在尝试用一个按钮制作一个应用程序。

我正在使用 Intellij Design 工具进行布局,一切看起来都不错,但是当我尝试运行应用程序时,我的屏幕上没有任何按钮。

这是我的活动文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        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="com.test.button test.MainActivity" tools:layout_editor_absoluteY="81dp"
        tools:layout_editor_absoluteX="0dp">

    <Button
            android:text="Hello"
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"
            tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1"
            tools:layout_constraintBottom_creator="1" tools:layout_constraintLeft_creator="1"/>
</android.support.constraint.ConstraintLayout>

编辑: 将 android:layout_width="0dp" 更改为 android:layout_width="wrap_content" 不会改变任何内容。

【问题讨论】:

  • android:layout_width="0dp" => android:layout_width="wrap_content"
  • 你有 android:layout_width="0dp"
  • 仍然无法使用 layout_width="wrap_content"

标签: android intellij-idea


【解决方案1】:

您为 Button 指定了宽度 0。所以这对你来说是不可见的。给它宽度。 使用下面的代码作为按钮;

  <Button
        android:id="@+id/join_now"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Join Now"/>

【讨论】:

  • 去掉这两行:tools:layout_editor_absoluteY="81dp" tools:layout_editor_absoluteX="0dp"
【解决方案2】:

这是因为您使用的是 ConstraintLayout,如果没有给出任何约束,它会将所有按钮重置为 (0,0)。 尝试使用 RelativeLayout 或设置约束。

【讨论】:

    猜你喜欢
    • 2022-11-13
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多