【问题标题】:How to Center a text in Android Studio?如何在 Android Studio 中将文本居中?
【发布时间】:2020-07-19 15:53:23
【问题描述】:

我正在制作我的应用,我想在 Android Studio 中将一段文本居中。

这是我目前正在使用的代码。

<activity
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:label="Login to Notely"
        android:name=".ActivityLogin"/>

我要做的就是将位于应用顶部的“登录到 Notely”文本居中。

有人可以帮我解决这个问题吗?提前致谢。

【问题讨论】:

  • 使用&lt;TextView&gt;

标签: android xml android-studio android-layout android-xml


【解决方案1】:

ActivityApplication 的子元素,表示必须在AndroidManifest. xml 文件中定义的activity。它有许多属性,例如标签、名称、主题、启动模式等。android:label 代表一个标签,即显示在屏幕上。 它还执行一些其他任务:

  • 它负责通过提供权限来保护application 访问任何受保护的部分。
  • 它还声明了应用程序将要使用的 android API
  • 它列出了检测类。检测类提供分析和其他信息。这些信息在应用程序发布等之前被删除。

有一点很清楚,&lt;activity&gt; 与组织文本无关。

现在要将activity 文本居中,您必须首先在XML 文件中定义ViewGroup(LinearLayout、RelativeLayout、ConstrainLayout 等)。在这个例子中,我使用RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
      android:text="Hello World"
      />

</RelativeLayout>

【讨论】:

  • 你的回答很有帮助,但我应该把这段代码放在哪里?还有一件事情。除了文字,你能放一张图片吗?这对我很有帮助。
  • 要放置图像,您必须将 替换为 ,更多详细信息请访问developer.android.com/reference/android/widget/ImageView。其次,如果回答有帮助,请标记为已接受。
猜你喜欢
  • 2014-09-02
  • 2021-09-29
  • 2020-11-21
  • 1970-01-01
  • 2015-12-26
  • 2011-06-14
  • 2014-08-08
  • 2014-07-21
  • 2011-06-12
相关资源
最近更新 更多