[英]Android: how to programmatically get layout's width
Well, the title is not really exhaustive but it's how I would fix the problem. 好吧,标题并不是真的很详尽,但这是我要解决的问题。 If there are other solutions, they are welcome. 如果还有其他解决方案,欢迎使用。
I'm testing my app in 2 emulators and this is the output: 我正在2个模拟器中测试我的应用,这是输出:
It's a row of an ExpandableListView and contains a title on the left and a preview on the right. 它是ExpandableListView的一行,左侧包含一个标题,右侧包含一个预览。 The preview is supposed to be cut if too long. 如果预览时间过长,则应该将其剪切。 The problem is that in the first case it's too short and in the second one too long. 问题在于,在第一种情况下,它太短,而在第二种情况下,它太长。
This is the layout of the row: 这是该行的布局:
<?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:background="@drawable/elv_title_1_selector"
android:gravity="fill_horizontal|center"
android:orientation="horizontal">
<ImageView
android:id="@+id/evl_grp_indicator"
android:layout_width="16dp"
android:layout_height="16dp"
android:contentDescription="indDown"
android:src="@drawable/indicator_selector" />
<Button
android:id="@+id/cbWarning"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/warning_selector"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:textColor="@color/LogoButtonText"/>
<TextView
android:id="@+id/txtGroupName"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:enabled="true"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text=""
android:textColor="@drawable/elv_title_1_selector"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="40dp"
android:gravity="center_vertical"
android:padding="5dp"
android:textSize="12sp" />
</LinearLayout>
I've set the text size of the preview to 12sp and I programmatically cut the text if longer than n chars. 我已将预览的文本大小设置为12sp,并且如果长度超过n个字符,则会以编程方式剪切文本。 What I need is to cut it larger on the first case and shorter on the second. 我需要在第一种情况下将其切大,在第二种情况下将其切短。
Any ideas how to ? 有什么想法吗?
To fix this issue add an attribute android:singleLine="true"
and android:ellipsize="end"
for the textview
. 要解决此问题,请为textview
添加属性android:singleLine="true"
和android:ellipsize="end"
。
<TextView
android:id="@+id/txtPreview"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:enabled="true"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text=""
android:textColor="@drawable/elv_title_1_selector"
android:singleLine="true"
android:ellipsize="end"
android:textSize="16sp"
android:textStyle="bold" />
First of all add android:singleLine="true"
and android:ellipsize="end"
in your second TextView
首先,在第二个TextView
添加android:singleLine="true"
和android:ellipsize="end"
<TextView
...
android:singleLine="true"
android:ellipsize="end"
...
/>
for Managing the text size on different device 用于在不同设备上管理文本大小
create folder 创建文件夹
values
values-v21
values-sw600
and add dimes.xml
and specify your textsize
for different resolutions 并添加dimes.xml
并指定textsize
为不同的分辨率
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.