我正在显示带有Timer和Ok,Cancel选项的对话框。但是计时器没有对齐为中心,计时器显示在左下角。
DatetimecustomActivity.java
package com.example.datetimecustom;
import java.util.Calendar;
import DateTimePicker.DateTimePicker.DateWatcher;
import DateTimePicker.DateTimePicker;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class DatetimecustomActivity extends Activity implements DateWatcher{
EditText edit_text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datetimecustom);
edit_text = (EditText) findViewById(R.id.edittext1);
}
public void button_click(View view){
// Create the dialog
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final LinearLayout mDateTimeDialogView = (LinearLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
// Update demo TextViews when the "OK" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.clearFocus();
// TODO Auto-generated method stub
String result_string = String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());
// if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
// else result_string = result_string + "AM";
edit_text.setText(result_string);
mDateTimeDialog.dismiss();
}
});
// Cancel the dialog when the "Cancel" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimeDialog.cancel();
}
});
// Setup TimePicker
// No title on the dialog window
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(mDateTimeDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
mDateTimeDialog.show();
mDateTimeDialog.getWindow().setAttributes(lp);
// Display the dialog
mDateTimeDialog.show();
}
public void onDateChanged(Calendar c) { // goi khi co thay doi tu calendar
Log.e("",
"" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)
+ " " + c.get(Calendar.YEAR));
}
}
date_time_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DateTimeDialog" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<DateTimePicker.DateTimePicker
android:id="@+id/DateTimePicker" android:layout_width="fill_parent"
android:padding="5dp"
android:layout_weight="2"
android:layout_height="0dp"
/>
<LinearLayout android:id="@+id/ControlButtons"
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dip">
<Button android:id="@+id/CancelDialog" android:layout_width="0dp"
android:text="@android:string/cancel" android:layout_weight="1"
android:layout_height="wrap_content" />
<Button android:id="@+id/SetDateTime" android:layout_width="0dp"
android:text="@android:string/ok" android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
datetimepicker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="5dip" android:id="@+id/DateTimePicker"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/hour_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/hour_plus"
android:layout_width="40dp"
android:layout_height="28dp"
android:background="@drawable/image_button_up" />
<EditText
android:id="@+id/hour_display"
android:layout_width="45dp"
android:layout_height="35dp"
android:background="@drawable/picker_middle"
android:gravity="center"
android:inputType="number"
android:textColor="#C0C0C0"
android:singleLine="true" >
</EditText>
<Button
android:id="@+id/hour_minus"
android:layout_width="40dp"
android:layout_height="28dp"
android:background="@drawable/image_button_down"/>
</LinearLayout>
<LinearLayout
android:id="@+id/min_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/min_plus"
android:layout_width="40dp"
android:layout_height="28dp"
android:background="@drawable/image_button_up" />
<EditText
android:id="@+id/min_display"
android:layout_width="45dp"
android:layout_height="35dp"
android:background="@drawable/picker_middle"
android:gravity="center"
android:inputType="number"
android:textColor="#C0C0C0"
android:singleLine="true" />
<Button
android:id="@+id/min_minus"
android:layout_width="40dp"
android:layout_height="28dp"
android:background="@drawable/image_button_down"/>
</LinearLayout>
</LinearLayout>
这是我的实际输出:
答案 0 :(得分:2)
这样做
更新 date_time_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DateTimeDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<DateTimePicker.DateTimePicker
android:id="@+id/DateTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ControlButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<Button
android:id="@+id/CancelDialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/cancel" />
<Button
android:id="@+id/SetDateTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok" />
</LinearLayout>
</LinearLayout>
答案 1 :(得分:0)
请更改以下布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DateTimeDialog" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<DateTimePicker.DateTimePicker
android:id="@+id/DateTimePicker" android:layout_width="fill_parent"
android:padding="5dp"
android:layout_weight="2"
android:gravity="center_horizontal"
android:layout_height="0dp"
/>
<LinearLayout android:id="@+id/ControlButtons"
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dip">
<Button android:id="@+id/CancelDialog" android:layout_width="0dp"
android:text="@android:string/cancel" android:layout_weight="1"
android:layout_height="wrap_content" />
<Button android:id="@+id/SetDateTime" android:layout_width="0dp"
android:text="@android:string/ok" android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>