2017-10-26 3 views
0

私はArrayAdapterというかなり単純なダイアログを持っています。私は、このダイアログを表示するとAlertDialog内のAndroidスイッチウィジェット、サム画像を表示しない(代わりにオン/オフ)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

    <TextView 
     android:id="@+id/txt_description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_centerVertical="true" 
     android:layout_toStartOf="@+id/calendar_selection_switch" 
     android:ellipsize="end" 
     android:text="test test" 
     android:textColor="@color/black" 
     android:textSize="20sp" /> 

    <Switch 
     android:id="@id/calendar_selection_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="end|center_vertical" 
     android:layout_marginStart="10dp" 
     android:minHeight="40dp" 
     android:minWidth="80dp" /> 
</RelativeLayout> 

が、これは私が得るものです::アダプターは、次のようなレイアウトを膨張

AlertDialog without the thumb image

お知らせ小さな文字でと「オン「と」オフ」の言葉。ここにカスタムグラフィックスやクラスはありません。すぐに使用できます。

Activityでこれを行うと、私はそれを知りません。代わりに、私は私がしたい正確に何である通常の親指の画像を取得:

enter image description here

私はスイッチのサイズを育てる場合でも、それはどんな違いがありません。リストの行を大きくするだけです。

ここでダイアログを構築し、表示するコードです。

AlertDialog.Builder builderSingle = new AlertDialog.Builder(this); 
builderSingle.setTitle(R.string.cal_title); 

builderSingle.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.dismiss(); 
    } 
}); 

final CalendarListAdapter cla = new CalendarListAdapter(this, calendarList, null); 
builderSingle.setAdapter(cla, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     cla.toggleSelect(which); 
    } 
}); 
builderSingle.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     if (saveCalendars(cla)) { 
      dialog.dismiss(); 
     } 
    } 
}); 
AlertDialog dialog = builderSingle.create(); 
dialog.show(); 

CalendarListAdapterシンプルArrayAdapterです:

public class CalendarListAdapter extends ArrayAdapter<Calendar> { 

このクラスのActivityバージョンとAlertDialogバージョン間に違いががありません。

私は困惑しています。私がここでやっていることはあまり複雑ではありません。助けてください。

答えて

0

このお試しください:

<Switch 
     android:id="@+id/calendar_selection_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="end|center_vertical" 
     android:layout_marginStart="10dp" 
     android:minHeight="40dp" 
     android:minWidth="80dp" 
     android:textOn="" 
     android:textOff="" 
     android:switchMinWidth="48.0sp" 
     android:thumb="@drawable/switch_selector" 
     android:track="@drawable/switch_track" 
     android:checked="false"/> 

switch_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_checked="true"> 
     <shape 
      android:shape="rectangle" 
      android:visible="true" 
      android:useLevel="false"> 
      <gradient 
       android:startColor="@color/white" 
       android:endColor="@color/white" 
       android:angle="270"/> 
      <corners 
       android:radius="14.0sp"/> 
      <size 
       android:width="28.0sp" 
       android:height="28.0sp" /> 
      <stroke 
       android:width="4.0sp" 
       android:color="#11000000"/> 
     </shape> 
    </item> 
    <item android:state_checked="false"> 
     <shape 
      android:shape="rectangle" 
      android:visible="true" 
      android:dither="true" 
      android:useLevel="false"> 
      <gradient 
       android:startColor="@color/white" 
       android:endColor="@color/white" 
       android:angle="270"/> 
      <corners 
       android:radius="16.0sp"/> 
      <size 
       android:width="28.0sp" 
       android:height="28.0sp" /> 
      <stroke 
       android:width="2.0sp" 
       android:color="#11000000"/> 
     </shape> 
    </item> 
</selector> 

switch_track.xml:あなたが行うことができ、コードで次に

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
    android:visible="true" 
    android:useLevel="false"> 
    <gradient 
     android:startColor="@color/white" 
     android:endColor="@color/white" 
     android:angle="270"/> 
    <stroke 
     android:width="2.0sp" 
     android:color="#11000000"/> 
    <corners 
     android:radius="14.0sp"/> 
    <size 
     android:width="42.0sp" 
     android:height="28.0sp" /> 
</shape> 

 aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        aSwitch.setTrackResource(R.drawable.switch_track_off); 
       } else { 
        aSwitch.setTrackResource(R.drawable.switch_track); 
       } 
      } 
     }); 

switch_track_off.xml:あなたが望むよう

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
    android:visible="true" 
    android:useLevel="false"> 
    <gradient 
     android:startColor="#00A4FD" 
     android:endColor="#00A4FD" 
     android:angle="270"/> 
    <stroke 
     android:width="2.0sp" 
     android:color="#00A4FC"/> 
    <corners 
     android:radius="14.0sp"/> 
    <size 
     android:width="42.0sp" 
     android:height="28.0sp" /> 
</shape> 

あなたはおよびその他のスタイルを変更することができます。

私が知る限り、Switchの外観と感触は、アプリ内のすべてのバージョンで同じAndroidで同じにする唯一の信頼できる方法です。

より良い提案がある場合は、コメントを残してください。

+0

これは私が尋ねてきた質問に対する答えだとは思わない。これは、AlertDialogの中にあるときと、アクティビティの中にあるときに、スイッチが異なって見えるのはなぜですか? – Emmanuel

+0

それ以外の方法がない場合は、これを回避策として使用することがあります。 – Emmanuel

+0

@Emmanuelは 'AlertDialog'の' support library'バージョンを使用していますか?私はそれが「図書館」の「引出し」を壊すことによると思う。 –

関連する問題