在XML中设置Android TimePicker

Pij*_*usn 19 layout android timepicker

是否可以在XML文件中将TimePicker小时模式设置为24小时模式?或者它只适用于Java?我想制作一个24小时选择器的布局,但我找不到这样的属性.

Gui*_*ume 43

不,你不能在XML中设置24小时模式,你必须使用

MyTimePicker.setIs24HourView(boolean);
Run Code Online (Sandbox Code Playgroud)


小智 11

通过子类化TimePicker可以获得24小时版本,并在XML文件中使用具有相应包名称的子类:

public class TimePicker24Hours extends TimePicker {

    public TimePicker24Hours(Context context) {
        super(context);
        init();
    }

    public TimePicker24Hours(Context context,
            AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TimePicker24Hours(Context context,
            AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setIs24HourView(true);
    }

}
Run Code Online (Sandbox Code Playgroud)

在您可能添加的布局中

   <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:dt_codecomponents="http://schemas.android.com/apk/res/my.app.package"
    ...

      <my.app.package.style.TimePicker24Hours
            android:id="@+id/timePicker1"
            android:layout_width="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)