首页 > 解决方案 > Android Studio:在RelativeLayout中的编辑模式期间未显示按钮

问题描述

我已经尝试为这个问题寻找许多解决方案,但没有一个对我有用。这是我的主要活动的 .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 是否有问题。任何帮助将不胜感激!

标签: javaandroidxmlandroid-studio

解决方案


app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

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

android:layout_below="@id/rl_all_day"

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

android:layout_alignParentStart="true"

不需要同时使用 alignPargentStart 和 alignParentLeft


推荐阅读