android8.1添加3个Textview到LinearLayout中,权重都为1,在默认显示大小下没事,改设置里显示大小为最大时3个Textview不一样大

建成OS 2018-10-08 05:48:23
LinearLayout代码:
<LinearLayout
android:id="@+id/call_log_sub_tab_hw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/call_log_sub_tab_padding_top"
android:paddingBottom="@dimen/call_log_sub_tab_padding_bottom"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:orientation="horizontal">
<!-- add sub tab at CallLogFragmentHw -->
</LinearLayout>



CallLogFragmentHw添加Textiew部分代码, 如图:
        subTabLayout = (LinearLayout) view.findViewById(R.id.call_log_sub_tab_hw);

//init sub tab, add text view
for (int i = 0; i < mTabTitles.length; i++) {
TextView textView = new TextView(getActivity());
LogUtil.d("CallLogFragmentHw.setupView()", "tab i=" + i + "; rtl tab i=" + getRtlPosition(i));
if (getRtlPosition(i) == 0) {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_left_selector);
} else if (getRtlPosition(i) == mTabTitles.length - 1) {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_right_selector);
} else {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_middle_selector);
}
textView.setText(mTabTitles);
textView.setGravity(Gravity.CENTER);
textView.setTextAppearance(R.style.CallLogSubTabTextStyle);
subTabLayout.addView(textView);
textView.setOnClickListener(this);
if (startingTab == i) {
subTabSelected(textView, true);
}
}




style代码:
  <style name="CallLogSubTabTextStyle">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">@dimen/sub_tab_hw_text_size</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
<item name="android:enabled">true</item>
</style>


















...全文
403 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
æ¼ 2018-10-15
  • 打赏
  • 举报
回复
TextView textView = new TextView(new ContextThemeWrapper(getActivity(), R.style.CallLogSubTabTextStyle));
Android小码家 2018-10-09
  • 打赏
  • 举报
回复
很明显textView.setTextAppearance(R.style.CallLogSubTabTextStyle); 这行代码无效,你可以自定义一个TextView 在其构造函数中加入这行代码 或者getlayoutparam重新设置一下 宽度和权重
jklwan 2018-10-09
  • 打赏
  • 举报
回复
引用 7 楼 zjc_jiancheng 的回复:
你好,吃完饭回来验证了一下,发现还是一样的。
抱歉,自己没测试,确实无效。 代码设置style确实比较麻烦,需要自己去读取style的样式中的每个值再去设置,一般都是新建xml文件,设置样式,再去获取View。
建成OS 2018-10-09
  • 打赏
  • 举报
回复
谢谢大家!已经解决了,确实如6楼说的,textView.setTextAppearance(R.style.CallLogSubTabTextStyle); 这句设置宽高没用,
解决方法:
subTabLayout = (LinearLayout) view.findViewById(R.id.call_log_sub_tab_hw);

//init sub tab, add text view
for (int i = 0; i < mTabTitles.length; i++) {
TextView textView = new TextView(getActivity());
LogUtil.d("CallLogFragmentHw.setupView()", "tab i=" + i + "; rtl tab i=" + getRtlPosition(i));
if (getRtlPosition(i) == 0) {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_left_selector);
} else if (getRtlPosition(i) == mTabTitles.length - 1) {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_right_selector);
} else {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_middle_selector);
}
textView.setText(mTabTitles[i]);
textView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textView.setLayoutParams(lp);

//textView.setTextAppearance(R.style.CallLogSubTabTextStyle);
subTabLayout.addView(textView);
textView.setOnClickListener(this);
if (startingTab == i) {
subTabSelected(textView, true);
}
}
YXTS122 2018-10-09
  • 打赏
  • 举报
回复
楼主能否把效果图展示一下
建成OS 2018-10-08
  • 打赏
  • 举报
回复
引用 6 楼 jklwan 的回复:
看你代码是用代码设置样式,但是用的是textView.setTextAppearance(R.style.CallLogSubTabTextStyle);
这句代码无法设置宽高等,只是设置的字体颜色,大小等等,如果要用代码初始化View并使用style的话用下面这句代码。
TextView textView = new TextView(new ContextThemeWrapper(getActivity(), R.style.CallLogSubTabTextStyle));

和在xml中使用style="R.style.CallLogSubTabTextStyle"效果相同。


subTabLayout = (LinearLayout) view.findViewById(R.id.call_log_sub_tab_hw);

//init sub tab, add text view
for (int i = 0; i < mTabTitles.length; i++) {
//TextView textView = new TextView(getActivity());
TextView textView = new TextView(new ContextThemeWrapper(getActivity(), R.style.CallLogSubTabTextStyle));

LogUtil.d("CallLogFragmentHw.setupView()", "tab i=" + i + "; rtl tab i=" + getRtlPosition(i));
if (getRtlPosition(i) == 0) {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_left_selector);
} else if (getRtlPosition(i) == mTabTitles.length - 1) {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_right_selector);
} else {
textView.setBackgroundResource(R.drawable.call_log_sub_tab_bg_middle_selector);
}
textView.setText(mTabTitles[i]);
textView.setGravity(Gravity.CENTER);
//textView.setTextAppearance(R.style.CallLogSubTabTextStyle);
subTabLayout.addView(textView);
textView.setOnClickListener(this);
if (startingTab == i) {
subTabSelected(textView, true);
}
}

你好,吃完饭回来验证了一下,发现还是一样的。
jklwan 2018-10-08
  • 打赏
  • 举报
回复
看你代码是用代码设置样式,但是用的是textView.setTextAppearance(R.style.CallLogSubTabTextStyle); 这句代码无法设置宽高等,只是设置的字体颜色,大小等等,如果要用代码初始化View并使用style的话用下面这句代码。
TextView textView = new TextView(new ContextThemeWrapper(getActivity(), R.style.CallLogSubTabTextStyle));
和在xml中使用style="R.style.CallLogSubTabTextStyle"效果相同。
建成OS 2018-10-08
  • 打赏
  • 举报
回复
手机是720x1280的默认density是 320, 正常默认进去界面是3个Textview平均,大小一样的。
但是在设置里改了显示里面的显示大小之后,界面被放大,用adb shell wm density 命令查看发现:
Physical density: 320
Override density: 360

density好像变为了360,然后再进界面,就前2个Textview大,第3个Textview,挤在最后很小,变成了不平均,大小不一样了
建成OS 2018-10-08
  • 打赏
  • 举报
回复
引用 3 楼 z979451341 的回复:
你把layout_widht改成0dp,让权重自己去算
各个View的大小是首先按照各个layout_widht 和layout_height来计算的,然后父布局剩下的位置由各个view的weight来瓜分


你好,<item name="android:layout_width">0dp</item> 这个没错啊,是设置的0dp
键盘舞者113 2018-10-08
  • 打赏
  • 举报
回复
你把layout_widht改成0dp,让权重自己去算
各个View的大小是首先按照各个layout_widht 和layout_height来计算的,然后父布局剩下的位置由各个view的weight来瓜分
建成OS 2018-10-08
  • 打赏
  • 举报
回复
问题在这: android8.1添加3个Textview到LinearLayout中,权重都为1,在默认显示大小下没事,改设置里显示大小为最大时3个Textview不一样大
键盘舞者113 2018-10-08
  • 打赏
  • 举报
回复
你是问啥问题

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧