【问题标题】:XML markup in the Android applicationAndroid 应用程序中的 XML 标记
【发布时间】:2022-01-03 16:06:15
【问题描述】:

如何分配对象,使 TextView 完全位于父对象的中间,而 ImageButton 位于左侧。尝试为父元素安装

android:gravity="center"

,以及 TextView 本身

android:layout_gravity="center"

,但什么都没有出来。我附上下面的代码。

<?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=".add">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="67dp"
        android:orientation="horizontal"
        android:gravity="center">

        <ImageButton
            android:id="@+id/backArrow"
            android:layout_width="30dp"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:background="@color/transparent"
            android:src="@drawable/back_arrow" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/tenor_sans"
            android:text="@string/adder"
            android:textColor="#2C1E4D"
            android:textSize="24sp" />

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

标签: java android xml android-layout


【解决方案1】:

这对您的要求有帮助吗

<?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=".add">

 <LinearLayout
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="67dp"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/backArrow"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:background="@color/colorBlack"
        android:scaleType="fitCenter"
        android:src="@drawable/back_arrow" />

    <TextView
        android:layout_gravity="center|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/aclonica"
        android:text="@string/adder"
        android:textColor="#2C1E4D"
        android:textSize="24sp" />

 </LinearLayout>

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2013-04-02
    • 2017-01-04
    • 2012-03-31
    • 2011-04-12
    相关资源
    最近更新 更多