【问题标题】:Linearlayout weights not giving what I write线性布局权重没有给出我写的内容
【发布时间】:2019-07-20 13:30:18
【问题描述】:

所以我想要一个标题总是占屏幕的三分之一,然后在屏幕的其余三分之二的地方加上名字。看起来很简单,但结果是,如果有很多名字,标题会变成不到三分之一。我只写了两行代码 //1 //2 以便轻松找到它们,但给出整个代码,因为它可能会影响结果。

这是一张照片。 The top text does not show the whole message and Is not 1 third of the screen as intended 提前致谢!

    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);
    }

【问题讨论】:

  • 添加 param.height=0 没有帮助。添加了结果照片,点击蓝色文字

标签: android android-linearlayout android-layout-weight


【解决方案1】:

尝试使用加权线性布局

 <LinearLayout
       ....
       ...weightSum="3"
       ...orientation="vertical">

使用

将顶视图的权重设置为2
<...
   ...layout_weight="1">

下面的设置权重为2

【讨论】:

  • 我正在动态添加我的物品并尝试了重量总和,但不幸的是它没有工作
  • 您是否将项目动态添加到列表视图或回收器视图?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多