android:gravity =“ center”问题,预览与模拟器不同

时间:2019-07-14 18:36:39

标签: android android-layout

当前,我有此页面的代码。

但是我的预览应该使我的按钮位于下半部分布局的中心,但是在我的模拟器中,它显示它仍位于布局的顶部。

render() {
  return (
    <B someProperty={'someString'} />
  );
}

在此处预览图片

https://i.stack.imgur.com/QLaot.jpg

这是模拟器的图像

https://i.stack.imgur.com/ED3FP.jpg

3 个答案:

答案 0 :(得分:0)

不同的手机具有不同的屏幕尺寸,在您的布局中,您使用的是固定尺寸的视图(例如,固定尺寸为android:layout_width="100dp"),结果是看起来效果不错一个屏幕(您的android studio预览屏幕)在另一屏幕(您的实际手机)上看起来效果不佳。

如果要创建一种布局以支持所有屏幕尺寸,可以将ConstraintLayoutguidelinesChains结合使用以支持不同的屏幕尺寸。

以下是使用ConstaintLayout的示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">


<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent=".5" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline7"
    app:layout_constraintEnd_toStartOf="@+id/guideline9"
    app:layout_constraintStart_toStartOf="@+id/guideline8"
    app:layout_constraintTop_toTopOf="parent"
    tools:srcCompat="@tools:sample/avatars[1]" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Email"
    app:layout_constraintEnd_toEndOf="@+id/imageView2"
    app:layout_constraintStart_toStartOf="@+id/imageView2"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline9"
    app:layout_constraintStart_toStartOf="@+id/guideline8"
    app:layout_constraintTop_toTopOf="@+id/guideline7" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="Button"
    app:layout_constraintEnd_toStartOf="@+id/guideline9"
    app:layout_constraintStart_toStartOf="@+id/guideline8"
    app:layout_constraintTop_toBottomOf="@+id/button" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent=".1" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent=".9" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是它的外观(我正在从布局编辑器附加图像,以便您可以看到约束和准则):

enter image description here

答案 1 :(得分:0)

您可以使用标签ax = plt.gca(),这样就不必担心屏幕大小不同。在这里,我使用fig, ax = plt.subplots() data.plot(kind='line', x='time', y='accel_y', ax=ax) ax.set_xlabel("Time (s)") ax.set_ylabel("Normalised Vertical Acceleration") android:layout_weight制作了一个需要的结构的示例。希望对您有帮助。该结构的屏幕截图为here

您可以从here获得有关android:layout_weight, android:layout_gravity的更多参考。

android:gravity

答案 2 :(得分:0)

您似乎做对了。我猜您只是错过了在父标记中添加weightSum。

<?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"
    android:orientation="vertical"
    android:weightSum="2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_profile">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/default_person_icon"
            app:civ_border_color="@android:color/black"
            app:civ_border_width="2dp"
            android:layout_centerInParent="true"
            android:layout_marginTop="100dp" a/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="User Email"
            android:textSize="28sp"
            android:textColor="@android:color/white"
            android:layout_below="@+id/profile_img"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <Button
            android:id="@+id/btn_change_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Change Password"
            app:cornerRadius="50dp"
            android:layout_marginStart="40dp"
            android:layout_marginEnd="40dp"/>

        <Button
            android:id="@+id/btn_sign_out"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sign Out"
            app:cornerRadius="50dp"
            android:layout_marginStart="40dp"
            android:layout_marginEnd="40dp"/>

    </LinearLayout>

</LinearLayout>

这似乎使它对我有用。祝一切顺利!

相关问题