0

晚上好!我正在尝试在我构建的自定义视图上 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;
}
}
4

1 回答 1

1

只是为了推断我的评论,您的自定义View对象有责任尊重设置的填充。您可以执行以下操作以确保您处理这种情况:

测量()

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);

布局()

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
于 2013-06-23T21:48:32.413 回答