簡體   English   中英

Android:如何以編程方式獲取布局的寬度

[英]Android: how to programmatically get layout's width

好吧,標題並不是真的很詳盡,但這是我要解決的問題。 如果還有其他解決方案,歡迎使用。

我正在2個模擬器中測試我的應用,這是輸出:

1024x600 mdpi:

480x800 hdpi:

它是ExpandableListView的一行,左側包含一個標題,右側包含一個預覽。 如果預覽時間過長,則應該將其剪切。 問題在於,在第一種情況下,它太短,而在第二種情況下,它太長。

這是該行的布局:

<?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>

我已將預覽的文本大小設置為12sp,並且如果長度超過n個字符,則會以編程方式剪切文本。 我需要在第一種情況下將其切大,在第二種情況下將其切短。

有什么想法嗎?

要解決此問題,請為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" />

首先,在第二個TextView添加android:singleLine="true"android:ellipsize="end"

<TextView 
...
 android:singleLine="true"
android:ellipsize="end"
...
/>

用於在不同設備上管理文本大小

創建文件夾

values
values-v21
values-sw600

並添加dimes.xml並指定textsize為不同的分辨率

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM