TimePicker对我来说很好,除了在横向视图中,它从右边裁剪了一点而没有显示整个时钟,或者时钟甚至不可见,只是给我背景颜色(左边是粉红色和相反,右边是白色的。试图寻找所有解决方案,因为"没有设置标题",我没有设置标题,甚至尝试将标题设置为空,没有工作。
xml文件
<?xml version="1.0" encoding="utf-8"?>
<TimePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TimePicker>
使用这些行调用
FragmentManager fm = getFragmentManager();
TimePickerFragment dialog =
TimePickerFragment.newInstance( mTimeString );
dialog.setTargetFragment( NewBlockFragment.this, REQUEST_TIME );
dialog.show(fm, DIALOG_TIME);
和timepicker类
public class TimePickerFragment extends DialogFragment {
public static final String EXTRA_TIME = "com.x";
private static final String ARG_TIME = "time";
private TimePicker mTimePicker;
public static TimePickerFragment newInstance(String time) {
Bundle args = new Bundle();
args.putSerializable(ARG_TIME, time);
TimePickerFragment fragment = new TimePickerFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String time = getArguments().getString(ARG_TIME);
int hour = Integer.parseInt(time.split(":", -1)[0]);
int minute = Integer.parseInt(time.split(":", -1)[1]);
View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_time, null);
mTimePicker = (TimePicker) v.findViewById(R.id.dialog_time_picker);
mTimePicker.setIs24HourView(true);
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mTimePicker.setHour(hour);
mTimePicker.setMinute(minute);
} else {
mTimePicker.setCurrentHour(hour);
mTimePicker.setCurrentMinute(minute);
}
return new AlertDialog.Builder(getActivity())
.setView(v)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int hour;
int minute;
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hour = mTimePicker.getHour();
minute = mTimePicker.getMinute();
} else {
hour = mTimePicker.getCurrentHour();
minute = mTimePicker.getCurrentMinute();
}
sendResult(Activity.RESULT_OK, hour + ":" + minute);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
private void sendResult(int resultCode, String time) {
if (getTargetFragment() == null) {
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_TIME, time);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, intent);
}
}
我得到的主要logcat例外
09-22 13:04:25.847 1784-2256/com.android.systemui E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7fb429023080
09-22 13:04:25.853 2825-2825/com.x D/android.widget.GridLayout: horizontal constraints: x1-x0>=503, x2-x1>=816, x2-x0<=864 are inconsistent; permanently removing: x2-x0<=864
答案 0 :(得分:0)
请尝试一下:
timePicker.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
答案 1 :(得分:0)
试试此代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.<YOUR_XML_FILE_NAME>);
更改以下代码:
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.<YOUR_XML_FILE_NAME>);
}
答案 2 :(得分:0)
通过完全更改&#34; timepickerfragment.java&#34;来修复它。代码,仍然不确定第一个问题出了什么问题,但是对于那些不想在接下来的四天里寻找答案的人来说这是我在几个问题板上找不到的
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.TimePicker;
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
public static final String EXTRA_TIME =
"com.x.time";
private static final String ARG_TIME = "time";
private TimePicker mTimePicker;
public static TimePickerFragment newInstance(String time) {
Bundle args = new Bundle();
args.putSerializable(ARG_TIME, time);
TimePickerFragment fragment = new TimePickerFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String time = getArguments().getString(ARG_TIME);
int hour = Integer.parseInt(time.split(":", -1)[0]);
int minute = Integer.parseInt(time.split(":", -1)[1]);
// TODO this for 24-hours, make the final false for AM/PM or make it variable
return new TimePickerDialog(getActivity(), this, hour, minute, true);
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
sendResult(Activity.RESULT_OK, hourOfDay + ":" + minute);
}
private void sendResult(int resultCode, String time) {
if (getTargetFragment() == null) {
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_TIME, time);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, intent);
}
}