问题描述
晚上好!我想setPadding上的自定义视图我建立与本土setPadding()什么也没做,所以我写了我自己......过了一会儿,我意识到,setPadding被调用我原来的电话后好几次,我不知道为什么。 ..请帮助:)(我知道我的自定义setPadding也许很过分的^^)
下面是包含查看XML。这是饼图。
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / PieDialog_llParent
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><的TextView
机器人:ID =@ + ID / PieDialog_tvHeader
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:比重=中心
机器人:文字=头
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?<的TextView
机器人:ID =@ + ID / PieDialog_tvDiv1
机器人:layout_width =match_parent
机器人:layout_height =2DP
机器人:TEXTSIZE =0SP/><的TextView
机器人:ID =@ + ID / PieDialog_tvDiv2
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =0SP/>< com.SverkerSbrg.Spendo.Statistics.Piechart.PieChart
机器人:ID =@ + ID / PieDialog_Pie
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT/><的TextView
机器人:ID =@ + ID / PieDialog_tvDiv3
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =0SP/><的FrameLayout
机器人:ID =@ + ID / PieDialog_flClose
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <的TextView
机器人:ID =@ + ID / PieDialog_tvClose
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:比重=中心
机器人:文字=大文本/>< /&的FrameLayout GT;< / LinearLayout中>
这里是code,其中我使用xml:
包com.SverkerSbrg.Spendo.Transaction.TransactionList.PieDialog;进口...公共类PieDialog扩展SpendoDialog {
私人TransactionSet transactionSet;
私人TransactionGroup transactionGroup;
私人GUI_attrs gui_attrs;
私人PieData pieData;私人饼图馅饼;
私人TextView的tvHeader;
公共对话框onCreateDialog(捆绑savedInstanceState){
AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
LayoutInflater吹气= getActivity()getLayoutInflater()。 查看查看= 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); 饼图馅饼=(饼图)view.findViewById(R.id.PieDialog_Pie);
pie.setPadding(40,10,40,10);
builder.setView(视图);
AlertDialog广告= builder.create(); 返回的广告;
}
公共无效初始化(GUI_attrs gui_attrs,TransactionSet transactionSet,长groupIdentifier){
this.gui_attrs = gui_attrs;
this.transactionSet = transactionSet;
}
}
只是对我的评论推断,这是你的自定义查看
对象的责任尊重填充是组。你可以像下面这样来确保您处理这种情况:
onMeasure()
INT desiredWidth,desiredHeight;desiredWidth = //确定你是多么需要宽
desiredWidth + = getPaddingLeft()+ getPaddingRight();desiredHeight = //确定你是多么需要高度
desiredHeight + = getPaddingTop()+ getPaddingBottom();INT是measuredHeight,measuredWidth可以;//检查对MeasureSpec - 如果它是MeasureSpec.EXACTLY,或MeasureSpec.AT_MOST
//遵循这些限制,以确定测量尺度setMeasuredDimension(是measuredWidth,measuredHeight可以);
onLayout()
INT左偏移= getPaddingLeft();
INT topOffset = getPaddingTop();//布局你的孩子(如有的话)根据左侧和顶部偏移,
//而不仅仅是0,0
的onDraw()
canvas.translate(getPaddingLeft(),getPaddingTop());//现在绘制的东西,正常
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;
}
}
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
这篇关于构造函数之后setPadding(0,0,0,0)多次调用上查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!