我对android dev很新,我正在尝试学习绳索。为了做到这一点,我一直在搞乱Android中的自定义视图。我正在尝试构建一个闹钟应用程序,我想做一个漂亮的微调器来选择时间。类似于这样说:
http://i47.tinypic.com/aymyjc.jpg
我创建了一个AndroidScollSpinner类,如下所示:
public class AndroidScrollSpinner extends View {
public AndroidScrollSpinner(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBackground(canvas);
drawSomeText(canvas);
canvas.restore();
}
private void drawSomeText(Canvas canvas) {
Paint titlePaint = new Paint();
titlePaint.setColor(Color.BLUE);
canvas.drawTextOnPath("Bert", new Path(), 0.0f,0.0f, titlePaint);
}
private void drawBackground(Canvas canvas) {
Paint backgroundPaint = new Paint();
backgroundPaint.setColor(getResources().getColor(R.color.bluegrass));
backgroundPaint.setStyle(Paint.Style.FILL);
Bitmap background = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
canvas.drawBitmap(background, 0, 0, backgroundPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int w = Math.max(minw, MeasureSpec.getSize(widthMeasureSpec));
int h = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(w, h);
}
}
我遇到的问题是onMeasure
MeasureSpec.getSize(widthMeasureSpec)总是返回0.有谁知道为什么?或者我在这里缺少什么?
这也是我的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:id="@+id/addAlarmSpinnerLayout">
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<ToggleButton android:id="@+id/sundayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/S"
android:textOff="@string/S"/>
<ToggleButton android:id="@+id/mondayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/M"
android:textOff="@string/M"/>
<ToggleButton android:id="@+id/tuesdayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/T"
android:textOff="@string/T"/>
<ToggleButton android:id="@+id/wednesdayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/W"
android:textOff="@string/W"/>
<ToggleButton android:id="@+id/thursdayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/T"
android:textOff="@string/T"/>
<ToggleButton android:id="@+id/fridayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/F"
android:textOff="@string/F"/>
<ToggleButton android:id="@+id/saturdayToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:textOn="@string/S"
android:textOff="@string/S"/>
</LinearLayout>
<Button android:id="@+id/doneButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:onClick="onDoneClicked">
</Button>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hour24Clock"
android:id="@+id/hour24switch"
android:layout_gravity="center"
android:enabled="true"
android:onClick="createSpinners"/>
</LinearLayout>
如果您需要了解有关该应用的任何其他信息,请查看https://github.com/rdsmallwood928/NeverLate
下的内容我也意识到可能有第三方库可以用来获得我想要的微调器效果。但是,我真的这样做是一个学习练习,所以更重要的是要理解为什么这个代码总是返回0而不是从其他地方注入一个自定义组件并继续我的生活。在此先感谢您的帮助!
编辑:这是创建微调器的AddAlarmFragment类
public class AddAlarmFragment extends Fragment {
private AndroidClickSpinner minuteSpinner;
private AndroidClickSpinner hourSpinner;
private AndroidClickSpinner amPmSpinner;
private ToggleButton mondayToggle;
private ToggleButton tuesdayToggle;
private ToggleButton wednesdayToggle;
private ToggleButton thursdayToggle;
private ToggleButton fridayToggle;
private ToggleButton saturdayToggle;
private ToggleButton sundayToggle;
private Switch hour24Switch = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.add_alarm, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
createSpinners(view);
}
@Override
public void onResume() {
super.onResume();
}
public void createSpinners(View view) {
LinearLayout layout = (LinearLayout) view.findViewById(R.id.addAlarmSpinnerLayout);
hour24Switch = (Switch) view.findViewById(R.id.hour24switch);
layout.removeAllViews();
ArrayList<Object> hours = new ArrayList<>();
if(hour24Switch.isChecked()) {
for(int i=0;i<24;i++) hours.add(i);
} else {
for(int i=1;i<=12;i++) hours.add(i);
}
hourSpinner = new AndroidClickSpinner(getActivity(), hours);
layout.addView(hourSpinner);
ArrayList<Object> minutes = new ArrayList<>();
for(int i=0;i<=60;i++) minutes.add(i);
minuteSpinner = new AndroidClickSpinner(getActivity(), minutes);
layout.addView(minuteSpinner);
if(!hour24Switch.isChecked()) {
ArrayList<Object> amPm = new ArrayList<>();
amPm.add("AM");
amPm.add("PM");
amPmSpinner = new AndroidClickSpinner(getActivity(), amPm);
layout.addView(amPmSpinner);
}
mondayToggle = (ToggleButton) view.findViewById(R.id.mondayToggleButton);
tuesdayToggle = (ToggleButton) view.findViewById(R.id.tuesdayToggleButton);
wednesdayToggle = (ToggleButton) view.findViewById(R.id.wednesdayToggleButton);
thursdayToggle = (ToggleButton) view.findViewById(R.id.thursdayToggleButton);
fridayToggle = (ToggleButton) view.findViewById(R.id.fridayToggleButton);
saturdayToggle = (ToggleButton) view.findViewById(R.id.saturdayToggleButton);
sundayToggle = (ToggleButton) view.findViewById(R.id.sundayToggleButton);
//Prevent no day selected...
switch (new LocalDate().getDayOfWeek()) {
case 1:
mondayToggle.setSelected(true);
break;
case 2:
tuesdayToggle.setSelected(true);
break;
case 3:
wednesdayToggle.setSelected(true);
break;
case 4:
thursdayToggle.setSelected(true);
break;
case 5:
fridayToggle.setSelected(true);
break;
case 6:
saturdayToggle.setSelected(true);
break;
case 7:
sundayToggle.setSelected(true);
break;
}
PieChart pie = new PieChart(getActivity());
Resources res = getResources();
pie.addItem("Agamemnon", 2, res.getColor(R.color.seafoam));
pie.addItem("Bocephus", 3.5f, res.getColor(R.color.chartreuse));
pie.addItem("Calliope", 2.5f, res.getColor(R.color.emerald));
pie.addItem("Daedalus", 3, res.getColor(R.color.bluegrass));
pie.addItem("Euripides", 1, res.getColor(R.color.turquoise));
pie.addItem("Ganymede", 3, res.getColor(R.color.slate));
layout.addView(pie);
layout.addView(new AndroidScrollSpinner(getActivity()));
}
public Integer getHours() {
Integer hour = Integer.parseInt(hourSpinner.getSelectedItem().toString());
if(!hour24Switch.isChecked()) {
if(hour == 12) {
hour = 0;
}
if(amPmSpinner.getSelectedItem().equals("PM")) {
hour = hour + 12;
}
}
return hour;
}
public Integer getMinutes() {
return Integer.parseInt(minuteSpinner.getSelectedItem().toString());
}
public boolean[] getDays() {
boolean[] days = new boolean[7];
days[0] = mondayToggle.isChecked();
days[1] = tuesdayToggle.isChecked();
days[2] = wednesdayToggle.isChecked();
days[3] = thursdayToggle.isChecked();
days[4] = fridayToggle.isChecked();
days[5] = saturdayToggle.isChecked();
days[6] = sundayToggle.isChecked();
return days;
}
}
答案 0 :(得分:0)
对不起,您在布局中使用它的位置是什么? 你是从代码中添加它,因为我看不到它包含在XML布局文件中的某个地方