gpt4 book ai didi

android - 如何在android中设置DatePickerDialog的标题布局

转载 作者:行者123 更新时间:2023-11-30 00:33:30 36 4
gpt4 key购买 nike

我正在 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);
}
}

刚刚测试,这是结果:

enter image description here

关于android - 如何在android中设置DatePickerDialog的标题布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43832860/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com