【问题标题】:setPadding(0,0,0,0) called several times on View after constructorsetPadding(0,0,0,0) 在构造函数后在 View 上调用了几次
【发布时间】:2013-06-24 05:48:32
【问题描述】:

晚上好!我正在尝试在我构建的自定义视图上 setPadding 并且本机 setPadding() 什么也没做,所以我自己编写了...过了一会儿,我意识到 setPadding 在我最初的调用后被调用了几次,我不知道为什么。 ..请帮助:)(我意识到我的自定义 setPadding 可能非常过分^^)

这是包含视图的 XML。这是饼图。

<?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;
}
}

【问题讨论】:

    标签: android padding


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 2019-08-02
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2021-11-23
      • 2019-12-02
      • 1970-01-01
      • 2011-05-08
      相关资源
      最近更新 更多