linearLayout未设置为垂直方向

时间:2013-01-21 22:37:45

标签: android orientation android-linearlayout

我正在扩展LinearLayout类,但方向存在一些问题。 我正在添加此视图,我正在创建其他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);
    }
}

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

1 个答案:

答案 0 :(得分:2)

在扩展LinearyLayout setOrientation(LinearLayout.VERTICAL);

的类的构造函数中调用此方法