作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 android 中创建 DatePickerDialog,我希望我的 datepickerdialog 看起来像图像中左侧的对话框。但是当我自己编写代码时,它只是水平显示文本日期(右边的对话框)我应该怎么办? My image
package com.example.luc.myapplication;
import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.app.Fragment;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.DatePicker;
import android.app.Dialog;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
*/
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Use the current date as the default date in the date picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
//Do something with the date chosen by the user
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
view.setLayoutParams(layout.getLayoutParams());
TextView tv = (TextView) getActivity().findViewById(R.id.tv);
tv.setText("Date changed...");
tv.setText(tv.getText() + "\nYear: " + year);
tv.setText(tv.getText() + "\nMonth: " + month);
tv.setText(tv.getText() + "\nDay of Month: " + day);
String stringOfDate = day + "/" + month + "/" + year;
tv.setText(tv.getText() + "\n\nFormatted date: " + stringOfDate);
}
}
我在 MainActivity 中点击一个按钮时调用对话框
最佳答案
我会先把你的布局改成这样,它会给你更多的控制权
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#008d7f"
android:paddingBottom="2dp"
android:paddingTop="5dp"
android:text="Friday"
android:textColor="#ffffff"
android:gravity="center_horizontal"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#009688"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:id="@+id/week_day"
android:textColor="#ffffff"
android:textSize="30sp"
android:text="MAY"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/day_of_month"
android:textSize="60sp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#ffffff"
android:text="8"
android:gravity="center_horizontal"
android:layout_below="@+id/week_day"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/year"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="2017"
android:gravity="center_horizontal"
android:layout_below="@+id/day_of_month"/>
</RelativeLayout>
然后将 java 更改为:
package com.example.luc.myapplication;
import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.app.Fragment;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.DatePicker;
import android.app.Dialog;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
*/
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Use the current date as the default date in the date picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
//Do something with the date chosen by the user
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
view.setLayoutParams(layout.getLayoutParams());
TextView tv_dayofweek = (TextView) getActivity().findViewById(R.id.day_of_week);
TextView tv_month = (TextView) getActivity().findViewById(R.id.month);
TextView tv_dayofmonth = (TextView) getActivity().findViewById(R.id.day_of_month);
TextView tv_year = (TextView) getActivity().findViewById(R.id.day_of_week);
tv.setText("Date changed...");
tv_year.setText(tv.getText() + "\nYear: " + year);
tv_month.setText(tv.getText() + "\nMonth: " + month);
tv_dayofmonth.setText(tv.getText() + "\nDay of Month: " + day);
String stringOfDate = day + "/" + month + "/" + year;
tv.setText(tv.getText() + "\n\nFormatted date: " + stringOfDate);
}
}
刚刚测试,这是结果:
关于android - 如何在android中设置DatePickerDialog的标题布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43832860/
我是一名优秀的程序员,十分优秀!