我想为每个安卓设备做一个设计。为此,我使用具有高度的 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>
图片:
最佳答案
我可以通过编程方式解决我的问题:
此代码在我的 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/