2013-06-23 15 views
0

之後在View上多次調用!我試圖setPadding在我建立的自定義視圖和本機setPadding()沒有做任何事情,所以我寫了我自己的......過了一段時間後,我意識到setPadding在我的原始調用後被調用了幾次,我不知道爲什麼。 ..請幫助:)(我知道我的自定義setPadding可能相當多^^)setPadding(0,0,0,0)在構造函數

這裏是包含視圖的XML。這是PieChart。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/PieDialog_llParent" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/PieDialog_tvHeader" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="Header" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/PieDialog_tvDiv1" 
    android:layout_width="match_parent" 
    android:layout_height="2dp" 
    android:textSize="0sp"/> 

<TextView 
    android:id="@+id/PieDialog_tvDiv2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="0sp" /> 

<com.SverkerSbrg.Spendo.Statistics.Piechart.PieChart 
    android:id="@+id/PieDialog_Pie" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:id="@+id/PieDialog_tvDiv3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="0sp" /> 

<FrameLayout 
    android:id="@+id/PieDialog_flClose" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/PieDialog_tvClose" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Large Text" /> 

</FrameLayout> 

</LinearLayout> 

這裏是我使用的XML代碼:

package com.SverkerSbrg.Spendo.Transaction.TransactionList.PieDialog; 

imports... 

public class PieDialog extends SpendoDialog{ 
private TransactionSet transactionSet; 
private TransactionGroup transactionGroup; 
private GUI_attrs gui_attrs; 
private PieData pieData; 

private PieChart pie; 
private TextView tvHeader; 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    View view = inflater.inflate(R.layout.transaction_list_pie_dialog, null); 

    LinearLayout llParent = (LinearLayout) view.findViewById(R.id.PieDialog_llParent); 
    llParent.setBackgroundColor(gui_attrs.color_Z0); 

    tvHeader = (TextView) view.findViewById(R.id.PieDialog_tvHeader); 
    tvHeader.setTextSize(gui_attrs.textSize_header); 

    TextView tvDiv1 = (TextView) view.findViewById(R.id.PieDialog_tvDiv1); 
    tvDiv1.setBackgroundColor(gui_attrs.color_Z2); 

    TextView tvDiv2 = (TextView) view.findViewById(R.id.PieDialog_tvDiv2); 
    tvDiv2.setPadding(0, gui_attrs.padding_Z0, 0, 0); 

    PieChart pie = (PieChart) view.findViewById(R.id.PieDialog_Pie); 
    pie.setPadding(40, 10, 40, 10); 


    builder.setView(view); 
    AlertDialog ad = builder.create(); 

    return ad; 
} 
public void initialize(GUI_attrs gui_attrs, TransactionSet transactionSet, long groupIdentifier){ 
    this.gui_attrs = gui_attrs; 
    this.transactionSet = transactionSet; 
} 
} 

回答

1

就推斷我的評論,那是你的自定義View對象的責任尊重所設置的填充。你可以這樣做以下,以確保您處理這種情況:

onMeasure()

int desiredWidth, desiredHeight; 

desiredWidth = //Determine how much width you need 
desiredWidth += getPaddingLeft() + getPaddingRight(); 

desiredHeight = //Determine how much height you need 
desiredHeight += getPaddingTop() + getPaddingBottom(); 

int measuredHeight, measuredWidth; 

//Check against the MeasureSpec -- if it's MeasureSpec.EXACTLY, or MeasureSpec.AT_MOST 
//follow those restrictions to determine the measured dimension 

setMeasuredDimension(measuredWidth, measuredHeight); 

onLayout()

int leftOffset = getPaddingLeft(); 
int topOffset = getPaddingTop(); 

//layout your children (if any) according to the left and top offsets, 
//rather than just 0, 0 

的onDraw()

canvas.translate (getPaddingLeft(), getPaddingTop()); 

//Now draw your stuff as normal