2017-04-13 75 views
0

我在自定義對話框中有一個時間選擇器。這是結果:更改對話框中的timepicker背景顏色

enter image description here

這是我的代碼:

<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="android:background">#FFFFFF</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
    <item name="android:windowNoTitle">true</item> 
</style> 

final Dialog dialog = new Dialog(SabadKharid_s1.this, R.style.DialogStyler2); 
        dialog.setContentView(R.layout.dialogtime); 

        timePicker = (TimePicker) dialog.findViewById(R.id.timePicker); 
        timePicker.setIs24HourView(true); 

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
        lp.copyFrom(dialog.getWindow().getAttributes()); 
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
        dialog.show(); 
        dialog.getWindow().setAttributes(lp); 

正如你可以看到,在標題中的小時和分鐘textView的是白色的。我怎樣才能改變他們的背景顏色?

+0

我覺得你的對話框的背景色爲白色,因爲'<項目名稱= 「機器人:背景」> #FFFFFF'。嘗試將其改爲另一種顏色 –

回答

0

背景將無法正常工作,試試這個 -

<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="android:colorAccent">@color/your_accent</item> 
    <item name="android:colorPrimary">@color/your_primary</item> 
    <item name="android:colorPrimaryDark">@color/your_primary_dark</item> 
</style> 
0

您可以參考下面的代碼來獲取timepicker風格。

MainActivity.java

import static android.R.attr.button; 
public class MainActivity extends AppCompatActivity { 
    Calendar calendar ; 
    Button button; 
    TimePickerDialog timePickerDialog ; 
    int CalendarHour, CalendarMinute; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button = (Button)findViewById(R.id.button); 
     calendar = Calendar.getInstance(); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       CalendarHour = calendar.get(Calendar.HOUR_OF_DAY); 
       CalendarMinute = calendar.get(Calendar.MINUTE); 
       timePickerDialog = new TimePickerDialog(MainActivity.this, R.style.DialogTheme,new TimePickerDialog.OnTimeSetListener() { 
        @Override 
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
         Toast.makeText(MainActivity.this, "Time Selected"+selectedHour+":"+selectedMinute, Toast.LENGTH_SHORT).show(); 
        } 
       }, CalendarHour, CalendarMinute, true);//Yes 24 hour time 
       timePickerDialog.setTitle("Select Time"); 
       timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
        @Override 
        public void onCancel(DialogInterface dialogInterface) { 
         Toast.makeText(MainActivity.this, "Time Not Selected", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
       timePickerDialog.show(); 
      } 
     }); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CLICK HERE TO SHOW TIME PICKER DIALOG WITH MATERIAL DESIGN THEME" 
     android:id="@+id/button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

Style.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog"> 
    <item name="colorAccent">@color/purple</item> 
    <item name="colorControlNormal">@color/orange</item> 
</style> 
0

如果你改變的 '背景' 的顏色,而不是 'windowBackground' 出現這種情況。這爲我工作:

styles.xml

<style name="DialogTheme" parent="Theme.AppCompat.Dialog"> 
    <item name="android:windowBackground">#333</item> 
    <item name="colorAccent">#2BF</item> 
</style> 

MainActivity.java

Calendar cal = Calendar.getInstance(); 
TimePickerDialog tpd = new TimePickerDialog(
     this, 
     R.style.DialogTheme, 
     this, // must implement TimePickerDialog.OnTimeSetListener 
     cal.get(Calendar.HOUR_OF_DAY), 
     cal.get(Calendar.MINUTE), 
     false); 

tpd.show();