setPadding(0,0,0,0) called several times on View after constructor

大憨熊 提交于 2019-12-14 03:15:57

问题


Good evening! I'm trying to setPadding on a custom View i built and the native setPadding() did nothing so i wrote my own... After a while i realized that setPadding gets called several times after my original call and i have no idea why... Please help :) (I realize that my custom setPadding maybe quite excessive ^^)

Here is the XML containing the View. It's the 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>

And here is the code where i use the 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:


Just to extrapolate on my comment, it is your custom View object's responsibility to respect the padding that is set. You can do something like the following to make sure that you handle that case:

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


来源:https://stackoverflow.com/questions/17264788/setpadding0-0-0-0-called-several-times-on-view-after-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!