首页 > 解决方案 > 如何使用线性布局将视图定位到边框

问题描述

我需要将视图放在边框中,以便它位于布局的最左侧,并希望将其应用于所有不同手机的尺寸!?

例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="285dp"
        android:text="@string/textView"
        android:textSize="28sp" />

</LinearLayout>

现在此代码输出以下 Layout,我需要这样做而不使用边距来匹配所有手机尺寸

标签: androidlayoutview

解决方案


您可以将其添加到 android:layout_gravity="right"您的.TextViewandroid:orientation="vertical"LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"      
        android:text="@string/textView"
        android:textSize="28sp" />

</LinearLayout>

我希望它会和你一起工作。


推荐阅读