java TableLayout调整文本大小视图,即使文本大小固定
我在表格布局中有一些方形文本视图。我必须通过编程调整文本大小。无法使用新的“自动调整大小”,我需要自己设置文本大小
因此,我的方法是在表格布局中为文本视图设置固定的布局高度和布局宽度,并在代码中使用setTextSize
问题是,文本一调整大小,奇怪的事情就开始发生,不仅是正在编辑的文本视图/单元格,还有相邻的文本视图/单元格
第一次调整文本大小时,相邻单元格会受到影响(见图)。这在真实手机(API24)和模拟器(API24,28)上都会发生。第二次调整文本大小时,第二个单元格将在模拟器上恢复正常。在真正的设备上,第一个单元格的大小会再次调整,或者顶部边距会增加
我尝试更改TableLayout、TableRow和TextView的不同设置(wrap_content、min/max Height),但除了注释掉setTextSize之外,没有任何解决方法
我确实需要使用TableLayout,所以用不同的布局替换它不是我的选择
为什么这不管用
要复制问题,请在布局和代码下方粘贴到新的“空活动”项目中。在撰写本文时,我正在使用最新的软件(Android Studio 3.2.1和compile sdk版本API 28、java版本1.8、min sdk版本22)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:baselineAligned="false"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/mainContainer">
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/tableLayout1"
安卓:layout_gravity="center"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_margin="10dp"
安卓:background="#000">
<TableRow
安卓:id="@+id/tableRow1"
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_weight="1">
<TextView
安卓:layout_width="40dp"
安卓:layout_height="40dp"
安卓:background="#FFF"
安卓:textColor="#000"
安卓:id="@+id/textView1"
安卓:layout_margin="1dp"/>
<TextView
安卓:layout_width="40dp"
安卓:layout_height="40dp"
安卓:background="#FFF"
安卓:textColor="#000"
安卓:layout_margin="1dp"/>
</TableRow>
</TableLayout>
<Button
安卓:layout_gravity="bottom"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Click"
安卓:id="@+id/button1"/>
</LinearLayout>
我的活动代码是:
package app.howsmydriving.layoutproblem;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.TypedValue;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.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);
}
}
});
}
}
# 1 楼答案
我通过关闭LinearLayout、TableLayout和TableRow的baselineAligned来修复它:
不知道为什么会这样,如果有人有更好的解释,我会接受这个答案
# 2 楼答案
首先要理解的是
android:baselineAligned
的实际含义默认情况下,此属性设置为
true
,并控制水平LinearLayout
中兄弟TextView
的垂直位置。必要时将向上或向下推TextView
,以确保所有文本基线(文本所在的假想线)对齐接下来要认识到的是
TableRow
是LinearLayout
的一个子类,并且是水平的因此,如果您想完全控制
TextView
在TableRow
内的垂直位置,您应该设置android:baselineAligned="false"
以避免系统覆盖您编辑
TextView
的尺寸对基线对齐没有影响;LinearLayout
将(a)根据需要上下移动TextView
,以及(b)根据需要上下移动TextView
内的文本,以确保基线对齐这是一个演示。我已经设置了背景色,使一切都非常明显。父
LinearLayout
有一个固定的高度100dp
,第一个TextView
是wrap_content
,第二个是40dp
(比需要的大),第三个是16dp
(比需要的小)这一切都是为了确保基线一致。除非禁用基线对齐,否则
LinearLayout
将执行任何需要的操作