Android开发:TableRow中TextView文本部分被截断的问题求助
Android开发:TableRow中TextView文本部分被截断的问题求助
大家好,我正在用Android Studio Flamingo开发一款日语词典APP,目前遇到了一个布局问题想请教各位。
我用TableLayout来展示词典条目,每个条目对应一个TableRow,里面包含两个TextView:一个显示日语原词,另一个显示对应的中文翻译。我的需求是让原词的字号更大,但现在出现了一个问题——第二个TextView的内容会被截断,就像这样(注:原提问附有截断效果的截图)。
我猜测问题出在TextView的高度计算时机上:在把TextView添加到TableRow之前,高度就已经被计算好了,而添加之后两个TextView默认是基线对齐,大字号的原词TextView把翻译TextView的部分内容给挡住了。如果把原词的字号改小,这个问题就会消失,但我就是想要保留大字号的原词,请问有没有什么可行的解决办法?
以下是我的简化代码和布局文件:
Java初始化代码:
public void init_words_list() throws IOException { TableLayout stk = findViewById(R.id.table_main); TypedValue typedValue = new TypedValue(); getTheme().resolveAttribute(com.google.android.material.R.attr.colorOnSecondary, typedValue, true); int text_color = ContextCompat.getColor(this, typedValue.resourceId); String[] dict = { "学", "learning, education" }; double[] widths = { 0.6, 0.4 }; int[] heights = { 30, 20 }; TableRow tbrow = new TableRow(this); for (int i = 0; i < 2; i++) { TextView tv = new TextView(this); tv.setText(dict[i]); tv.setWidth((int) Math.round(widths[i] * (stk.getWidth()))); tv.setTextSize(heights[i]); tv.setTextColor(text_color); tv.setGravity(Gravity.START); tbrow.addView(tv); } stk.addView(tbrow); }
对应的布局XML:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"> <fragment android:id="@+id/nav_host_fragment_content_main" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/mobile_navigation" /> <ScrollView android:id="@+id/words_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorOnPrimary" android:layout_alignParentStart="true" tools:ignore="MissingConstraints"> <TableLayout android:id="@+id/table_main" android:layout_width="match_parent" android:layout_height="wrap_content"> </TableLayout> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout>
期待各位的解答,谢谢!
备注:内容来源于stack exchange,提问作者Vladislav
相关产品推荐
相关产品推荐

