2013-01-21 53 views
1

我正在擴展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); 
    } 
} 

我想看看它在模擬器和我的華碩平板兩者。 有什麼想法?謝謝!

+0

對不起這不回答你的問題,但我只是想知道你爲什麼會擴展任何類型的佈局/類的ViewGroup沒有提高其佈局/的ViewGroup功能。這不是真正的面向對象設計的工作原理。例如,爲什麼佈局/視圖組需要知道日期是什麼? – Squonk

+0

,因爲它會是一個圖形欄,當用戶持有它時,他會看到日期。 – vlio20

+1

我仍然不理解你的邏輯 - 你可以在佈局XML文件的'setParams()'方法中做所有事情,但是我認爲這取決於你如何去做。你的問題的一個可能的原因是'LinearLayout.LayoutParams(int width,int height)' - 你用'height,width'設置它。 – Squonk

回答

1

調用此方法可在擴展LinearyLayout類的構造函數setOrientation(LinearLayout.VERTICAL);

相關問題