[英]TableLayout resizing views on resizing text even though it is fixed size
我在TableLayout中有一些方形TextViews。 我必須以編程方式調整文本大小。 無法使用新的“自動調整大小”,我需要自行設置文本大小。
因此,我的方法是在表布局中為TextViews設置固定的layout_height和layout_width,並在代碼中使用setTextSize。
問題是,一旦調整了文本的大小,就會開始發生奇怪的事情,不僅是正在編輯的TextView /單元,而且相鄰的單元也是如此!
第一次調整文本大小時,相鄰單元格會受到影響(見圖)。 在真實電話(API24)和仿真器(API 24、28)上都會發生這種情況。 第二次調整文本大小時,第二個單元格將在模擬器上恢復正常。 在實際設備上,第一個單元格會再次調整大小,或者頂部邊距會增加。
我嘗試更改TableLayout,TableRow和TextView的不同設置(wrap_content,最小/最大高度),但是除了注釋掉setTextSize之外,沒有其他解決方法。
我確實需要使用TableLayout,所以用其他布局替換它不是我的選擇。
為什么不起作用?
要將問題粘貼粘貼到布局和代碼下面,請復制到新的“空活動”項目中。 在撰寫本文時,我正在使用最新的軟件(Android Studio 3.2.1,其編譯SDK版本為API 28,Java版本為1.8,最低SDK版本為22。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainContainer">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FFF"
android:textColor="#000"
android:id="@+id/textView1"
android:layout_margin="1dp"/>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FFF"
android:textColor="#000"
android:layout_margin="1dp"/>
</TableRow>
</TableLayout>
<Button
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/button1"/>
</LinearLayout>
我的活動代碼是:
package app.howsmydriving.layoutproblem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private String sContent = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
sContent += "A";
TextView tv = findViewById(R.id.textView1);
tv.setText(sContent);
if(sContent.length() < 3) {
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
} else {
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
}
}
});
}
}
首先要了解的是android:baselineAligned
實際含義。
默認情況下,此屬性設置為true
,並控制同級TextView
在水平LinearLayout
的垂直位置。 將根據需要向上或向下推TextView
,以確保所有文本基線(文本所在的假想線)對齊。
接下來要實現的是TableRow
是LinearLayout
的子類,並且是水平的。
因此,如果要完全控制TableRow
TextView
的垂直位置,則應設置android:baselineAligned="false"
以避免系統覆蓋您。
TextView
的尺寸對基線對齊沒有影響。 在LinearLayout
都將(一)移動TextView
小號自己上下必要的和(b)的范圍內移動文本TextView
上下和必要的,以確保基線對齊。
這是一個演示。 我設置了背景色,使所有內容都很明顯。 父級LinearLayout
的固定高度為100dp
,第一個TextView
為wrap_content
,第二個為40dp
(大於所需值),第三個為16dp
(小於所需值)。
這一切都是為了確保基線對齊。 除非禁用基線對齊,否則LinearLayout
會做任何必要的事情來實現這一目標。
我設法通過關閉LinearAlign,TableLayout和TableRow的baselineAligned來解決此問題:
android:baselineAligned="false"
不知道為什么會這樣,如果有人有更好的解釋,我會接受作為答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.