简体   繁体   中英

Android studio preview does not show layouts

I have just started to learn android studio. I've encountered a problem:

在此输入图像描述

The layouts won't appear in the preview after I have added them. But if I change the theme or run the app in the emulator they do appear but they overlap.

For API 27:

Add the following dependency: If you are using targetSdkVersion 27, in your App Level gradle file build.gradle(Module: app)

implementation 'com.android.support:appcompat-v7:27.1.1'

For API 28:

if you are using targetSdkVersion 28,

Step 1: Make sure you have the following dependency:

implementation 'com.android.support:appcompat-v7:28.0.0-rc02'

UPDATE: For the latest appcompat-v7 library repo version, follow this link

https://mvnrepository.com/artifact/com.android.support/appcompat-v7?repo=google

Step 2:

Goto Values > styles.xml and modify the style as follows,

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

or any other base theme you like.

Here is an example constraint layout for you,

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <EditText
        android:id="@+id/enter_age"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:ems="10"
        android:hint="enter age"
        android:inputType="number"
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button1"
        app:layout_constraintVertical_chainStyle="packed" />


    <Button
        android:id="@+id/button1"
        android:layout_width="200dp"
        android:layout_height="110dp"
        android:onClick="butGetAge"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/enter_age"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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