安卓布局 : Center Text and Image View to screen center?

标签 android xml layout textview imageview

我有一个启动画面的 Activity layout.xml,它基本上显示了我想要的内容。但是如果我在更大或更小的屏幕设备上打开应用程序,它就不再居中了。

有没有办法在元素周围放置一个包装器并将其在屏幕的高度和宽度上居中?还有其他选择吗?

    <?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"
    android:background="@color/colorPrimaryDark" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="250dp"
        android:layout_marginBottom="7dp"
        android:text="@string/head_line"
        android:textColor="@color/white"
        android:textSize="50sp"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher_round"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="7dp"
        android:text="@string/slogan"
        android:textColor="@color/white"
        android:textSize="24sp" />

</LinearLayout>

现在,它看起来像这样:

enter image description here

提前谢谢大家!

最佳答案

试试这个

<?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:background="@color/colorPrimaryDark"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="7dp"
        android:text="@string/head_line"
        android:textColor="@color/white"
        android:textSize="50sp"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="7dp"
        android:text="@string/slogan"
        android:textColor="@color/white"
        android:textSize="24sp" />

</LinearLayout>

关于安卓布局 : Center Text and Image View to screen center?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47579492/

相关文章:

android - 错误 : "Adb connection Error:An existing connection was forcibly closed by the remote host"

javascript - 如何解析此 XSL 文件并读取将其嵌入 HTML 文件中的元素

html - XML 中的换行符?

xaml - 在 xamarin 表单中缩放字体大小

css - 在两个 div(左和右)之间的 div 中居中文本

android - Unresolved 类 PlaceAutocompleteFragment

java - 如何将 EditText 中的所有单个整数相加?

android - 如何在 Kotlin 中使用 intent.setComponent

java - XMLStreamWriter 输出无效字符(不编码换页)

Java 图形不显示更改