两个 TextView 应占据最小高度
Two TextViews should occupy minimum height
我有一个水平对齐 2 TextView
s 的 LinearLayout。第一个必须左对齐,第二个必须在屏幕中右对齐(它们之间存在 space)。我希望文本填满整个屏幕,使其占用的空间尽可能少 space。另外,如果文本太短,它应该占据整个屏幕行,而不是一半。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
tools:text="Example of code" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:text="Sample text here, it shouldn't occupy much space" />
</LinearLayout>
如何在 XML 中执行此操作?
更新
第一个和第二个 TextView
的长度可以不同。
布局现在显示为:。
我希望它像这样:。
更新 2
比开始时好一点的布局。这不是我想要的,但解决了一个问题。
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
tools:text="Example of code" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="Sample text here, it shouldn't occupy much space" />
</LinearLayout>
如果您将它添加到您的两个 TextView 标签中,它会满足您的需要吗?
android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:id="@+id/text1"
android:gravity="start" //left allign
android:layout_width="0dp"
android:layout_marginEnd="10dp" // for spacing between two
android:layout_height="wrap_content"
tools:text="Example of code" />
<TextView
android:gravity="end" // right allign
android:layout_weight="1"
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_marginStart="10dp" // for spacing between two
android:layout_height="wrap_content"
tools:text="Sample text here, it shouldn't occupy much space" />
</LinearLayout>
等布局权重平分可用宽度,为需要更多的TextView增加权重space。
我不确定我是否得到你的问题,但你可以在这些文本视图之间添加一个视图,或者使用 RelativeLayout
作为父视图并在第二个文本子视图上设置 android:layout_alignParentRight="true"
;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/colorAccent"
tools:text="Example of code" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/colorAccent"
android:gravity="center_horizontal"
tools:text="long long long " />
</LinearLayout>
不知道我理解的对不对。试试右边的"text view"match_parent和gravity"end".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
tools:text="Example of code"
android:layout_marginRight="10dp" />
<TextView
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Sample text here, it should not occupy much space" />
</LinearLayout>
你好,你能检查一下这个吗,如果它不只是把你想要的格式发给我,我会帮你的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:layout_alignParentLeft="true"
tools:text="Awesome" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/nice"
android:textAlignment="textEnd"
android:layout_alignParentRight="true"
android:background="@color/colorAccent"
tools:text="Welcome"
android:gravity="end" />
</RelativeLayout>
谢谢
我有一个水平对齐 2 TextView
s 的 LinearLayout。第一个必须左对齐,第二个必须在屏幕中右对齐(它们之间存在 space)。我希望文本填满整个屏幕,使其占用的空间尽可能少 space。另外,如果文本太短,它应该占据整个屏幕行,而不是一半。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
tools:text="Example of code" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:text="Sample text here, it shouldn't occupy much space" />
</LinearLayout>
如何在 XML 中执行此操作?
更新
第一个和第二个 TextView
的长度可以不同。
布局现在显示为:
我希望它像这样:
更新 2
比开始时好一点的布局。这不是我想要的,但解决了一个问题。
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
tools:text="Example of code" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="Sample text here, it shouldn't occupy much space" />
</LinearLayout>
如果您将它添加到您的两个 TextView 标签中,它会满足您的需要吗?
android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:id="@+id/text1"
android:gravity="start" //left allign
android:layout_width="0dp"
android:layout_marginEnd="10dp" // for spacing between two
android:layout_height="wrap_content"
tools:text="Example of code" />
<TextView
android:gravity="end" // right allign
android:layout_weight="1"
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_marginStart="10dp" // for spacing between two
android:layout_height="wrap_content"
tools:text="Sample text here, it shouldn't occupy much space" />
</LinearLayout>
等布局权重平分可用宽度,为需要更多的TextView增加权重space。
我不确定我是否得到你的问题,但你可以在这些文本视图之间添加一个视图,或者使用 RelativeLayout
作为父视图并在第二个文本子视图上设置 android:layout_alignParentRight="true"
;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/colorAccent"
tools:text="Example of code" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/colorAccent"
android:gravity="center_horizontal"
tools:text="long long long " />
</LinearLayout>
不知道我理解的对不对。试试右边的"text view"match_parent和gravity"end".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
tools:text="Example of code"
android:layout_marginRight="10dp" />
<TextView
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Sample text here, it should not occupy much space" />
</LinearLayout>
你好,你能检查一下这个吗,如果它不只是把你想要的格式发给我,我会帮你的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:layout_alignParentLeft="true"
tools:text="Awesome" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/nice"
android:textAlignment="textEnd"
android:layout_alignParentRight="true"
android:background="@color/colorAccent"
tools:text="Welcome"
android:gravity="end" />
</RelativeLayout>
谢谢