ANDROID:对齐动态创建的数字选择器

时间:2015-06-02 14:31:45

标签: android alignment numberpicker

我正在创建一个警报对话框,其中包含两个数字选择器和两个文本视图,以选择月份和年份,文本视图只是标签。 我正在动态创建它们,因为填充min和max选项似乎更容易(不能用xml和布局充气器来完成)。我只是不能将两个选择器(及其标签)集中在警告对话框中。它们显示在极左和极右,使它非常难看。 我可能过度编码,但这里是:

final NumberPicker np1= new NumberPicker(this);
    final NumberPicker np2= new NumberPicker(this);
    final TextView t1=new TextView(this);
    final TextView t2=new TextView(this);

             t1.setText("Month");
    t1.setTextSize(20);

    t2.setText("Year");
    t2.setTextSize(20);
             np1.setMaxValue(12);
    np1.setMinValue(1);
    np2.setMaxValue(2030);
    np2.setMinValue(2000);      
    np1.setValue(Month);
    np2.setValue(Year);

    RelativeLayout linearLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20,20);
    RelativeLayout.LayoutParams numPicerParams1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams numPicerParams2=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    numPicerParams1.addRule(RelativeLayout.LEFT_OF);
    numPicerParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    linearLayout.setLayoutParams(params);
    linearLayout.addView(np1, numPicerParams1);
    linearLayout.addView(np2, numPicerParams2);
    linearLayout.addView(t1,numPicerParams1);
    linearLayout.addView(t2,numPicerParams2);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Chose month and year.");
    builder.setView(linearLayout);
    builder.setPositiveButton("OK",new DialogInterface.OnClickListener() 
    { (...)

2 个答案:

答案 0 :(得分:0)

试试这个:

class User(object):

    GENDER_MALE = 'mr'
    GENDER_FEMALE = 'ms'

    def __init__(self, title=None, first_name=None, last_name=None, is_guest=None,
             company_name=None, mobile=None, landline=None, email=None, password=None,
             fax=None, wants_sms_notification=None, wants_email_notification=None,
             wants_newsletter=None, street_address=None):

        self. title = title
        self.first_name = first_name
        self.last_name = last_name
        self.company_name = company_name
        self.mobile = mobile
        self.landline = landline
        self.email = email
        self.password = password
        self.fax = fax
        self.is_guest = is_guest
        self.wants_sms_notification = wants_sms_notification
        self.wants_email_notification = wants_email_notification
        self.wants_newsletter = wants_newsletter
        self.company_name = company_name
        self.street_address = street_address

答案 1 :(得分:0)

您的XML应如下所示:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">
    <TextView
        android:id="@+id/month_text"
        android:text="Month"
        android:layout_width="wrap_content"
        android:layout_alignLeft="@id/month_picker"
        android:layout_alignRight="@id/month_picker"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>


    <TextView
        android:id="@+id/day_text"
        android:text="Year" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/month_text"
        android:layout_alignLeft="@id/year_picker"
        android:layout_alignRight="@id/year_picker"
        android:gravity="center_horizontal"/>

    <NumberPicker
        android:id="@+id/month_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/month_text" />

    <NumberPicker
        android:id="@+id/year_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/day_text"
        android:layout_toRightOf="@id/month_picker"/>

</RelativeLayout>

这将进入作为警报对话框主体的父布局。

然后,在代码中,您可以这样做:

NumberPicker monthPicker = (NumberPicker) view.findViewById(R.id.month_picker);
NumberPicker yearPicker = (NumberPicker) view.findViewById(R.id.year_picker);

monthPicker.setMaxValue(12);
monthPicker.setMinValue(1);
yearPicker.setMaxValue(2030);
yearPicker.setMinValue(2000);