1

我正在扩展 LinearLayout 类,但方向存在一些问题。我正在将我正在创建的这个视图添加到其他线性布局中,但它仍然设置为水平。这是代码:

package com.simplemathgame;

import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TableLayout;

public class GraphBar extends LinearLayout {
    private Date date;
    private int totalDrills;
    private int rightDrills;
    private int score;
    private int height;
    private final int widht = 30;
    private final int totHeight = 300;
    LinearLayout.LayoutParams barParams;

    public GraphBar(Context context, int totalDrills, int rightDrills, Date date) {
        super(context);
        this.date = date;
        this.rightDrills = rightDrills;
        this.totalDrills = totalDrills;

        this.setScore();
        setParams();
        this.barParams = new LinearLayout.LayoutParams(0,this.widht);
    }

    private void setScore(){
        this.height = (this.rightDrills / this.totalDrills) * totHeight;
    }

    private void setParams(){
        this.barParams = new LinearLayout.LayoutParams(this.height,this.widht);
        this.barParams.setMargins(5, 0, 0, 0);
        this.setBackgroundColor(Color.CYAN);
        this.setOrientation(LinearLayout.VERTICAL);
        this.setGravity(Gravity.BOTTOM);
        this.setLayoutParams(this.barParams);
    }
}

我试图在模拟器和我的华硕平板电脑中看到它。有任何想法吗?谢谢!

4

1 回答 1

2

在扩展 LinearyLayout 的类的构造函数中调用此方法 setOrientation(LinearLayout.VERTICAL);

于 2014-02-13T10:38:47.497 回答