android - LinearLayout 使用权重属性(高度)截断TextView 文本

标签 android android-layout xamarin android-linearlayout

我想为每个安卓设备做一个设计。为此,我使用具有高度的 LinearLayouts 及其百分比解决方案。

屏幕被分成许多部分(带权重的线性布局)。在这些 LinearLayouts 中是元素,例如 TextView。

但是,如果我使用具有高度的 LinearLayout 可以切断底部的 TextView。

如何根据权重动态更改文本大小?

代码:

 <LinearLayout
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="0.04"
    android:weightSum="1">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.05799" />
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.86951">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="Benachrichtigungen"
            android:textStyle="bold"
            android:textSize="20sp"
            android:id="@+id/header"/>
    </LinearLayout>
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.0722106" />
</LinearLayout>

图片:

Image

最佳答案

我可以通过编程方式解决我的问题:

此代码在我的 fragment 中的 OnCreateView 方法中。它根据 TextView 的高度生成文本大小。

view.ViewTreeObserver.GlobalLayout += (sender, e) => 
{
    var headerTextView = view.FindViewById<TextView>(Resource.Id.header);
    var headerTextViewHeightInSp = PxToSp(view.Context, headerTextView.Height);

    headerTextView.SetTextSize(ComplexUnitType.Sp, headerTextViewHeightInSp - 5); // 5 is a offset (space between outter borderline and text)
};

关于android - LinearLayout 使用权重属性(高度)截断TextView 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800758/

相关文章:

c# - 使用 Xamarin 构建可移植 SignalR 组件

java - 读取 APK 中的文件

java - 如何在Android中进行hitTest、碰撞检测功能

android - 始终在 MapView 上显示缩放控件

Android - 将自定义对话框布局高度设置为 wrap_content

android - 在我的情况下如何使用动态名称访问资源?

android - 在 smack 4.2 中创建自定义节

android - 像 whatsapp、viber、facebook 聊天这样的气泡

c# - 如何在 Xamarin IOS C# 上的项目中添加设计

android - ScrollView 允许内容溢出