布局重力问题:android:layout_gravity =“ end”

时间:2019-06-14 02:20:29

标签: android android-layout

我刚开始学习Android Studio,在这里遇到了一些麻烦。在对代码的这种解释上,我应该看到Hello world保持在右侧,但他不想从左侧移动。这是预期的结果: result expected和我所拥有的result at the moment

<LinearLayout 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:visibility="visible"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:gravity="center"
        android:text="Hello World!" />

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

android:orientation="vertical"添加到您的LinearLayout中。默认方向是水平。

答案 1 :(得分:0)

您可以将 TextView重力设置为右侧,并使 LinearLayout方向如下所示水平

<LinearLayout 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:visibility="visible"
tools:context=".MainActivity"
orientation="horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:gravity="right"
    android:text="Hello World!" />

</LinearLayout>

答案 2 :(得分:0)

尝试以下代码:-

<LinearLayout 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:visibility="visible"
android:orientation="horizontal">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:gravity="end"
    android:text="Hello World!" />

</LinearLayout>
相关问题