【问题标题】:Android Studio: Buttons not showing up during editing mode in RelativeLayoutAndroid Studio:在RelativeLayout中的编辑模式期间未显示按钮
【发布时间】:2018-08-28 13:35:28
【问题描述】:

我已经尝试为这个问题寻找许多解决方案,但没有一个对我有用。这是我的主要活动的 .xml 代码:

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:text="9" />

</RelativeLayout>

这是我的 Java 代码:

package com.example.jacob.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

我不知道我的代码中是否缺少某些内容,或者 android studio 是否有问题。任何帮助将不胜感激!

【问题讨论】:

  • 删除app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"
  • 我刚刚应用了更改。什么也没发生。
  • 您的 IDE 好像有问题.....您尝试重新启动 Android Studio 了吗
  • 是的,还是什么都没有。

标签: java android xml android-studio


【解决方案1】:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

这些属性在ConstraintLayout中使用而不是RelativeLayout,而在relativelayout中如果要确定widget之间的相对位置,则应使用

android:layout_below="@id/rl_all_day"

如果你想确定孩子和父母之间的相对位置,你应该使用

android:layout_alignParentStart="true"

不需要同时使用 alignPargentStart 和 alignParentLeft

【讨论】:

  • 当我删除 alignParentLeft 时,它警告我不要这样做,并告诉我重新添加它以支持旧版本的 Android。另外,我删除了 ConstraintLayout 代码,但没有任何效果。
  • 精心制作。
  • 当我说 ConstrainingLayout 代码时,我的意思是 app:layout_constraintBottom_toBottomOf="parent" 和其他三个。
  • 当您删除 ConstraintLayout 代码时,您是否向 textview 添加了一些其他属性?比如android:layout_alignParentEnd="true",可能你的按钮被textview覆盖了。顺便说一句,你可以尝试 File->invalidate Caches / Restart... 来重启你的 Android Studio
【解决方案2】:

尝试使用LinearLayout 而不是RelativeLayout,因为LinearLayout 可以让您更多地接触编码并有助于创建更多动态内容。

这是LinearLayout的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:text="9" />

</LinearLayout>

要在 XML 中使用 View 标记或在相应元素中添加 Padding 之间留出空格。

添加填充:

android:padding="25dp"

【讨论】:

    【解决方案3】:

    如果预览在您的 IDE 中不起作用,请更改 gradle 依赖项,如下所示

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleDependency
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    testImplementation 'junit:junit:4.12'
    }
    

    解决方案是将支持库版本降级为alpha1

    【讨论】:

    • 在 build.gradle(Project) 文件中
    猜你喜欢
    • 2019-09-19
    • 2021-08-31
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    相关资源
    最近更新 更多