线性布局权重没有给出我写的东西



所以我想有一个标题,它总是屏幕的三分之一,然后是屏幕三分之二的其余部分。看起来很容易,但结果是,如果有很多名字,标题就会不到三分之一。只有 2 行可以做到这一点,我写//1//2 可以轻松找到它们,但给出整个代码,因为它可能会影响结果。

这是一张照片。顶部文本不显示整个消息,并且不是预期的屏幕的 1/3提前感谢!

    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

最新更新