2013-08-01 20 views
2

我創建了一個android應用程序,它顯示一個數字選擇器,它一切正常......但問題是與設計....當我在薑餅中運行應用程序數字選擇器看起來不錯...但是當我在冰淇淋三明治和果凍豆中運行相同的東西時,數字選擇器設計被改變,就像下面所示。Android數字選擇器默認設計更改果凍豆和冰淇淋sandwitch

誰能請告訴我如何保留默認數選取器的設計,是在薑餅在果凍豆

當運行在冰淇淋三明治果凍豆

enter image description here

運行時姜麪包

enter image description here

我使用內其數量選擇器被放置在自定義對話框中,如下面

import android.app.Activity; 
import android.app.Dialog; 
import android.graphics.drawable.ColorDrawable; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.NumberPicker; 

public class QuantityChangeDialog extends Dialog implements android.view.View.OnClickListener { 

public Activity c; 
public Dialog d; 
public Button save, cancel; 
NumberPicker np; 

public QuantityChangeDialog(Activity a) { 
super(a); 
// TODO Auto-generated constructor stub 
this.c = a; 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

requestWindowFeature(Window.FEATURE_NO_TITLE); 
this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
setContentView(R.layout.selecteditem_dialog); 
save = (Button) findViewById(R.id.btn_save); 
cancel = (Button) findViewById(R.id.btn_cancel); 
save.setOnClickListener(this); 
cancel.setOnClickListener(this); 
np = (NumberPicker) findViewById(R.id.qntypicker); 
np.setMaxValue(120); 
np.setMinValue(1); 
np.setValue(3); 

} 

@Override 
public void onClick(View v) { 
switch (v.getId()) { 
case R.id.btn_save: 
c.finish(); 
break; 
case R.id.btn_cancel: 
dismiss(); 
break; 
default: 
break; 
} 
dismiss(); 
} 
} 

回答

3

從文檔

引用如果當前主題源自給定的代碼主題小部件將當前值顯示爲可編輯的輸入字段,其上方是增加按鈕,下方是減少按鈕。長按按鈕可以快速更改當前值。點擊輸入字段可以輸入所需的值。

你需要設置你的主題是從Theme derieved一樣來回例如Theme.NoTitleBar.Fullscreen

activity_main.xml中

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <Button 
     android:id="@+id/button11" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Open" /> 

</RelativeLayout> 

dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:theme = "@style/cust_dialog" 
    android:layout_height="fill_parent" > 

    <NumberPicker 
     android:id="@+id/numberPicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="64dp" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/numberPicker1" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="98dp" 
     android:layout_toRightOf="@+id/numberPicker1" 
     android:text="Cancel" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button2" 
     android:layout_alignBottom="@+id/button2" 
     android:layout_marginRight="16dp" 
     android:layout_toLeftOf="@+id/numberPicker1" 
     android:text="Set" /> 

</RelativeLayout> 

然後顯示自定義對話框

public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener 
{ 
    private TextView tv; 
    static Dialog d ; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView) findViewById(R.id.textView1); 
     tv.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         tv.setTextColor(Color.RED); 
       } 
       else if (event.getAction() == MotionEvent.ACTION_UP) { 
         // set to normal color 
        tv.setTextColor(0); 
       } 

       return true; 
      } 


      }); 
     Button b = (Button) findViewById(R.id.button11); 
     b.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       show(); 
      } 
      }); 
      } 
    @Override 
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 

     Log.i("value is",""+newVal); 

    } 

    public void show() 
    { 


     final Dialog d=new Dialog(this,R.style.cust_dialog); 
     d.setTitle("NumberPicker"); 
     d.setContentView(R.layout.dialog); 
     Button b1 = (Button) d.findViewById(R.id.button1); 
     Button b2 = (Button) d.findViewById(R.id.button2); 
     final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1); 
     np.setMaxValue(100); 
     np.setMinValue(0); 
     np.setWrapSelectorWheel(false); 
     np.setOnValueChangedListener(this); 
     b1.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) { 
       tv.setText(String.valueOf(np.getValue())); 
       d.dismiss(); 
      }  
      }); 
     b2.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) { 
       d.dismiss(); 
      }  
      }); 
     d.show(); 


    } 
} 

Styles.xml

</style> 
    <style name="cust_dialog" parent="@android:style/Theme.NoTitleBar.Fullscreen"> 
</style> 

快照

enter image description here

+0

我已經設置爲Theme.NoTitleBar.Fullscreen的主題,但我仍然無法找到數字選擇器中的任何設計更改........它仍然顯示如第一張上圖所示.... .. – Denny

+0

您是否在清單中設置了主題並嘗試 – Raghunandan

+0

否...如何設置清單可以請告訴我..... – Denny

2

你可以只添加此屬性爲您NumberPicker

android:theme="@android:style/Theme.Dialog" 

例如

<NumberPicker android:theme="@android:style/Theme.Dialog" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

這會限制對數字選取器小部件的影響,而不是整個活動頁面。

相關問題