2017-10-17 143 views
1

我想为每个Android设备进行设计。为此,我使用LinearLayouts的高度和百分比解决方案。LinearLayout通过使用weight属性(高度)来切断TextView文本

屏幕被分成许多部分(带权重的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

+1

屏幕截图和您当前的xml可能有助于更快地回答这个问题。 –

+0

我已更新我的问题。谢谢你的建议。 :) –

+0

你需要约束你的身高吗?如果没有设置外部LinearLayout高度wrap_content也许。 –

回答

1

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

此代码是在我的片段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) 
};