我正在使用DatePickerDialog,它似乎在我的Note 5上没有正确缩放。它在屏幕中央看起来相当小。有没有办法告诉DatePickerDialog使用90%的屏幕?请参阅下面的代码:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="3dp"
android:minWidth="800dp"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/txtContract"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp"
android:ems="10"
android:hint="Contract #"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/txtTrip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp"
android:ems="10"
android:hint="Trip #"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/txtDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp"
android:ems="10"
android:hint="Date"
android:focusable="false"
android:inputType="date" >
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" >
<Button
android:id="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Start Trip" />
</LinearLayout>
</LinearLayout>
主要
public class MainActivity extends Activity implements OnClickListener {
EditText txtContract;
EditText txtTrip;
EditText txtDate;
private static final String CONTRACT_REGEX = "^([a-zA-Z0-9_-]){5}$";
private static final String TRIP_REGEX = "^([a-zA-Z0-9_-]){1,10}$";
private static final String DATE_REGEX = "^\\d{2}\\/\\d{2}\\/\\d{4}$";
private static final String PHONE_NUMBER = "1234567890";
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Initialize Views
txtContract = (EditText) findViewById(R.id.txtContract);
txtTrip = (EditText) findViewById(R.id.txtTrip);
txtDate = (EditText) findViewById(R.id.txtDate);
btnSend = (Button) findViewById(R.id.btnSend);
// Set Listeners
txtDate.setOnClickListener(this);
btnSend.setOnClickListener(this);
// Check If Device Is Capable Of Sending SMS
PackageManager pm = this.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) && !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
}
}
catch (Exception ex) {
Toast.makeText(this, "Error in MainActivity.onCreate: " + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
// Handle Clicked View
switch (v.getId()) {
// Date Field
case R.id.txtDate:
// Get Current Date
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
// Set Up DatePicker Dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// Define Calendar For Formatting
final Calendar cf = Calendar.getInstance();
// Format Selected Date
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
cf.set(year,month,day);
String selectedDate = sdf.format(cf.getTime());
// Add Selected Date To EditText Field
txtDate.setText(selectedDate);
}
}, mYear, mMonth, mDay);
// Set Max Date
c.add(Calendar.DATE, 1);
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
// Set Min Date
c.add(Calendar.DATE,-4);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
// Display DatePicker
datePickerDialog.show();
break;
// Submit Button
case R.id.btnSend:
// Define Variables
String strContract;
String strTrip;
String strDate;
String strMessage;
SmsManager sms;
Boolean rval = true;
// Get Input Strings
strContract = txtContract.getText().toString();
strTrip = txtTrip.getText().toString();
strDate = txtDate.getText().toString();
// Validate
if (!Validation.isValid(txtContract, CONTRACT_REGEX, "Invalid Contract #", true)) rval = false;
if (!Validation.isValid(txtTrip, TRIP_REGEX, "Invalid Trip #", true)) rval = false;
if (!Validation.isValid(txtDate,DATE_REGEX,"Invalid Date",true)) rval = false;
// Build Message
strMessage = strContract.concat("|").concat(strTrip).concat("|").concat(strDate);
// Continue If We Pass Validation
if (rval) {
try {
// Send SMS
sms = SmsManager.getDefault();
sms.sendTextMessage(PHONE_NUMBER, null, strMessage, null, null);
// Display Message
Toast.makeText(this, "Sent.", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(this, "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
// Clear Fields
txtContract.setText("");
txtTrip.setText("");
txtDate.setText("");
}
break;
}
}
}