[英]How to put views properly to make all views visible inside Linear Layout?
I have a horizontal linear layout with 3 elements, an icon and two text views: 我有一个水平线性布局,其中包含3个元素,一个图标和两个文本视图:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@drawable/icon_place" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:maxLines="1"
android:text="Information 1"
android:textColor="@android:color/black"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Information 2"
android:gravity="right"
android:textSize="13sp"
android:maxLines="1"
android:textColor="@android:color/darker_gray"
android:layout_margin="8dp"/>
</LinearLayout>
Giving me this : 给我这个:
I want the second text view "Information 2" to always stay visible, the first text view being cropped if needed. 我希望第二个文本视图“ Information 2”始终保持可见,如果需要,第一个文本视图将被裁剪。 But here is what happens with a long text in the first text view:
但是,在第一个文本视图中使用长文本会发生以下情况:
So what should I do instead? 那我该怎么办呢? Thanks for your help!
谢谢你的帮助!
Firstthing you set android:maxLines = 1 to enforce your textviews to visiable at one line. 首先,您将android:maxLines = 1设置为将textview强制显示为一行。 If you want your textview cropped itself based on content.
如果您希望textview根据内容进行裁剪。 Remove below code from xml.
从xml中删除以下代码。
android:maxLines="1"
Second thing your first textview
take remaining space of linear layout other than second textview
and imageview
you must use android:weight property of linearlayout. 第二件事,您的第一个
textview
会占用线性布局的剩余空间,而不是第二个textview
和imageview
您必须使用linearlayout的android:weight属性。 you can see the simple layout weight property as per your need here. 您可以在此处根据需要查看简单的布局权重属性。 https://developer.android.com/guide/topics/ui/layout/linear.html
https://developer.android.com/guide/topics/ui/layout/linear.html
Weightsum
equalweights
distributed weights
all you need for complex linear layouts. Weightsum
equalweights
distributed weights
了复杂线性布局所需的全部distributed weights
。 See this tutorial for help http://abhiandroid.com/ui/linear-layout 请参阅本教程以获取帮助http://abhiandroid.com/ui/linear-layout
Here is the Code 这是代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@drawable/ic_menu_send" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:text="Information 1 IInformation Information InformationInformationvvvInformation Information"
android:textColor="@android:color/black"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Information 2"
android:textSize="13sp"
android:maxLines="1"
android:textColor="@android:color/darker_gray"
android:layout_margin="8dp"/>
</LinearLayout>
Output: 输出:
In Editor View 在编辑器视图中
You can wrap your two TextViews in a horizontal LinearLayout splitting it into two equal size parts by using the weightsum attribute. 您可以将两个TextView包装在水平LinearLayout中,方法是使用weightsum属性将其拆分为两个相等大小的部分。 You can also ellipsize the text if it is too long.
如果文本太长,也可以将其省略。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@drawable/icon_place" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:maxLines="1"
android:gravity="center_horizontal"
android:layout_weight="1"
android:text="Information 1 with a very very long test"
android:ellipsize="end"
android:textColor="@android:color/black"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Information 2"
android:gravity="center_horizontal"
android:textSize="13sp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/darker_gray"
android:layout_margin="8dp"/>
</LinearLayout>
</LinearLayout>
This is the result: 结果如下:
There is a concept of "android: weigth = 1" if you add it to all 3 components. 如果将其添加到所有3个组件中,则存在“ android:weigth = 1”的概念。 then their width will always be 33.3%, and the width should not change when displaying long texts, if you want a width of about 10% 40% 40% then you will have to conjure with this tag, ksatai value is not accepted in the format 0.5 they need to write here in this format .5
那么它们的宽度将始终为33.3%,并且在显示长文本时该宽度不应更改,如果您希望宽度为10%左右40%40%,那么您将不得不与该标签搭配使用,格式0.5,他们需要以这种格式.5编写
You should set android:layout_weight="1"
to the TextView
in the middle and change match_parent
by wrap_content
for the 3rd TextView
. 您应该将
android:layout_weight="1"
设置为中间的TextView
,并通过wrap_content
更改第三个TextView
match_parent
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@drawable/ic_launcher_background" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:maxLines="1"
android:text="Information 1"
android:textColor="@android:color/black"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="right"
android:maxLines="1"
android:text="Information 2"
android:textColor="@android:color/darker_gray"
android:textSize="13sp" />
</LinearLayout>
Use this one - 用这个-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@android:drawable/ic_lock_idle_alarm" />
<TextView
android:layout_toRightOf="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:maxLines="1"
android:text="Information 1"
android:textColor="@android:color/black"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Information 2"
android:layout_marginLeft="28dp"
android:background="@android:color/white"
android:gravity="right"
android:textSize="13sp"
android:maxLines="1"
android:textColor="@android:color/darker_gray"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"/>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.