在果冻豆和冰淇淋sandwitch机器人数量选择器默认的设计变更果冻、机器人、冰淇淋、数量

2023-09-06 18:32:16 作者:栀染°

我已经创建了一个Android应用程序,它会显示一个数字选择器,这一切工作正常......但问题是设计....当我运行应用程序在姜饼数选择器看起来不错不错...... 。但,当我运行相同的东西在冰淇淋三明治和果冻豆的数量选择器的设计被更改一样,如下图所示。

谁能告诉我怎样留住了默认号码选取器的设计,是在姜饼果冻豆

当运行在冰淇淋三明治和果冻豆

当运行在姜面包

我使用的是自定义对话框中的号码选择器被放置时,code是下面给出

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

公共类QuantityChangeDialog扩展对话框实现android.view.View.OnClickListener {

公共活动℃;
公众对话D组;
公共按钮保存,取消;
NumberPicker NP;

公共QuantityChangeDialog(活性){
超级(一);
// TODO自动生成构造函数存根
this.c =一个;
}


@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
。this.getWindow()setBackgroundDrawable(新ColorDrawable(android.graphics.Color.TRANSPARENT));
的setContentView(R.layout.selecteditem_dialog);
节省=(按钮)findViewById(R.id.btn_save);
取消=(按钮)findViewById(R.id.btn_cancel);
save.setOnClickListener(本);
cancel.setOnClickListener(本);
NP =(NumberPicker)findViewById(R.id.qntypicker);
np.setMaxValue(120);
np.setMinValue(1);
np.setValue(3);

}

@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.btn_save:
c.finish();
打破;
案例R.id.btn_cancel:
解雇();
打破;
默认:
打破;
}
解雇();
}
}
 
原来真的有mm豆冰淇淋,这个也太赞了吧

解决方案

从文档中引用

如果当前主题从主题衍生的小部件presents的当前值与增量按钮的上方和下方减量按钮可编辑的输入字段。龙pressing按钮允许电流值的快速更换。轻敲输入字段允许在期望的值类型。

您需要设置你的主题是从主题就像来回例如 Theme.NoTitleBar.Fullscreen

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>


    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/参考hello world/>

    <按钮
        机器人:ID =@ + ID / button11
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:文本=打开/>

< / RelativeLayout的>
 

dialog.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:主题=@风格/ cust_dialog
    机器人:layout_height =FILL_PARENT>

    < NumberPicker
        机器人:ID =@ + ID / numberPicker1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =64dp/>

    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / numberPicker1
        机器人:layout_marginLeft =20dp
        机器人:layout_marginTop =98dp
        机器人:layout_toRightOf =@ + ID / numberPicker1
        机器人:文本=取消/>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID /按钮2
        机器人:layout_alignBottom =@ + ID /按钮2
        机器人:layout_marginRight =16DP
        机器人:layout_toLeftOf =@ + ID / numberPicker1
        机器人:文本=设置/>

< / RelativeLayout的>
 

然后显示自定义对话框

 公共类MainActivity扩展活动实现NumberPicker.OnValueChangeListener
{
    私人TextView的电视;
    静态对话框D组;
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        电视=(TextView中)findViewById(R.id.textView1);
        tv.setOnTouchListener(新OnTouchListener(){

            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                       tv.setTextColor(Color.RED);
                }
                否则如果(event.getAction()== MotionEvent.ACTION_UP){
                        //设置为正常颜色
                     tv.setTextColor(0);
                }

                返回true;
            }


            });
        按钮B =(按钮)findViewById(R.id.button11);
         b.setOnClickListener(新OnClickListener()
         {

            @覆盖
            公共无效的onClick(视图v){
                 显示();
            }
            });
           }
     @覆盖
    公共无效onValueChange(NumberPicker选择器,INT OLDVAL,诠释的newval){

         Log.i(值是,+的newval);

     }

    公共无效展()
    {


         最后一个对话框D =新的对话框(这一点,R.style.cust_dialog);
         d.setTitle(NumberPicker);
         d.setContentView(R.layout.dialog);
         按钮B1 =(按钮)d.findViewById(R.id.button1);
         按钮B2 =(按钮)d.findViewById(R.id.button2);
         最后NumberPicker NP =(NumberPicker)d.findViewById(R.id.numberPicker1);
         np.setMaxValue(100);
         np.setMinValue(0);
         np.setWrapSelectorWheel(假);
         np.setOnValueChangedListener(本);
         b1.setOnClickListener(新OnClickListener()
         {
          @覆盖
          公共无效的onClick(视图v){
              tv.setText(将String.valueOf(np.getValue()));
              d.dismiss();
           }
          });
         b2.setOnClickListener(新OnClickListener()
         {
          @覆盖
          公共无效的onClick(视图v){
              d.dismiss();
           }
          });
       d.show();


    }
}
 

Styles.xml

 < /风格>
  <样式名称=cust_dialog父=@安卓风格/ Theme.NoTitleBar.Fullscreen>
< /风格>
 

快拍

i've created an android application which displays a number picker, it all works fine...but the problem is with the design....when i run the application in gingerbread the number picker looks fine good....but when i run the same stuff in ice-cream sandwich and jelly bean the number picker design is been altered just like as shown below.

can anyone please tell me how to retain the default number-picker design that is in gingerbread in jelly bean

when runs in ice-cream sandwich and jelly bean

when runs in ginger-bread

i'm using a custom dialog box within which the number picker is placed, the code is as given below

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(); 
} 
}

解决方案

Quoting from docs

If the current theme is derived from Theme the widget presents the current value as an editable input field with an increment button above and a decrement button below. Long pressing the buttons allows for a quick change of the current value. Tapping on the input field allows to type in a desired value.

You need to set your theme that is derieved from Theme like fro example 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>

Then to display custom dialog

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>

Snap Shot