android - 线性布局权重没有给出我写的
问题描述
所以我想要一个标题总是占屏幕的三分之一,然后是屏幕其余三分之二的部分,并带有名称。看起来很简单,但结果是,如果有很多名字,标题会变成不到三分之一。我只写了 2 行代码 //1 //2 以便轻松找到它们,但给出整个代码,因为它可能会影响结果。
这是一张照片。 顶部的文字没有显示整个消息,也不是预期的屏幕的三分之一 提前谢谢!
private void showScores() {
float baseTextSize = 100/playerNum;
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT,
10000.0f //1
);
TextView t = new TextView(this);
String result_msg = getString(R.string.result_msg);
t.setLayoutParams(param);
t.setText(result_msg + "\n");
t.setTextColor(getColor(R.color.white));
if (playerNum>3) {
t.setTextSize(30f);
} else {
t.setTextSize(40f);
}
scoreShower.addView(t);
param.weight = 20000.0f/playerNum; //2
for (int i=0;i<playerNum;i++) {
TextView txt = new TextView(this);
txt.setLayoutParams(param);
if (i == 0) {
txt.setTextSize(baseTextSize + 8f);
txt.setTextColor(getColor(R.color.gold));
} else {
txt.setTextSize(baseTextSize);
txt.setTextColor(getColor(R.color.white));
}
String txtString = (i+1) + ". " + arrayList.get(i).getKey() + " : " + arrayList.get(i).getValue();
txt.setText(txtString);
if (playerNum>6) {
txt.setTextSize(10f);
}
scoreShower.addView(txt);
}
解决方案
尝试使用加权线性布局
<LinearLayout
....
...weightSum="3"
...orientation="vertical">
使用将顶视图的权重设置为 2
<...
...layout_weight="1">
下面的设置权重为2
推荐阅读
- c# - 无空格字符串中最常见的词
- css - Tailwind CSS 和 Alpine JS 过渡问题
- saml - 每 10 分钟向 IdP 发送 SAML AuthnRequest
- javascript - 如何控制用户的尝试次数并将其存储在数组中并在 JS 中显示数组?
- powerbi - Power BI:将列表转换为表格
- javascript - 反应时间实现中的秒静态
- pandas - 按自定义顺序订购情节图例
- c# - C# & ASP.NET MVC:如何在另一个模型中填充数据对象模型
- visual-studio - Visual Studio 2019:调试期间“本地”面板出错 - “检索本地值的内部错误”
- react-native - React-Redux 不更新 SetInterval 上的值