【问题标题】:ANDROID: Align dynamically created number pickersANDROID:对齐动态创建的数字选择器
【发布时间】:2015-06-02 23:07:14
【问题描述】:

我正在创建一个警报对话框,其中包含两个数字选择器和两个文本视图,用于选择月份和年份,文本视图只是标签。 我正在动态创建它们,因为填充 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() 
    { (...)

【问题讨论】:

  • 你能发布你的xml吗?
  • @JohnWhite - 我认为他正在动态创建所有内容。

标签: android alignment numberpicker


【解决方案1】:

试试这个:

RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.addRule(RelativeLayout.CENTER_IN_PARENT);


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

relativeLayout.addView(linearLayout);

【讨论】:

  • 这会使它们相互叠加,不是吗?
  • 看起来我不能像这样在 relativelayout 上使用 .addrule。Eclipse 说该方法未定义为 RelativeLayout 类型。
【解决方案2】:

您的 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);

【讨论】:

  • 我很抱歉,但我如何膨胀(调用)xml?警报对话框的正文内容是什么?
  • AlertDialog.Builder 为您提供了构建AlertDialog 的非常清晰的路径,您可以通过调用AlertDialog.Builder.setView(view) 来设置AlertDialog 的主体。传递给此方法的视图将是上面的 xml,它使用 LayoutInflater 进行扩展。
猜你喜欢
  • 2018-04-16
  • 2011-03-26
  • 2016-10-11
  • 2021-02-25
  • 2013-09-27
  • 2012-01-13
  • 2019-08-16
  • 1970-01-01
  • 2018-01-11
相关资源
最近更新 更多