【问题标题】:Button to add a custom layout to android application用于向 android 应用程序添加自定义布局的按钮
【发布时间】:2014-10-07 11:50:00
【问题描述】:

我有一个小的时间表应用程序,我添加了一个屏幕截图,因为它比试图解释它更容易展示给你!

希望从图片中您可以看到,工人所在的不同地点需要有一个工作编号,并且他们可以填写与每个工作编号相关的工作时间。所以这是我的问题我不知道他们每周会有多少工作编号,所以在这里我只添加了三个,但可能或多或少,我可以有一个“添加工作”按钮,每次它可以在下面添加一个工作视图是按下了吗?

以下是每次按下按钮时我想添加的部分的 xml 代码

<LinearLayout
    android:id="@+id/MainLayoutThree"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >  //start of 1st job layout

<LinearLayout
    android:id="@+id/LayoutTwoA"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/beige"
    android:orientation="horizontal" >

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

    <EditText
        android:id="@+id/editText3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>

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

    <EditText
        android:id="@+id/editText4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>

</LinearLayout>

<LinearLayout
    android:id="@+id/LayoutThree"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/beige"
    android:orientation="horizontal" >

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

</LinearLayout>

<LinearLayout
    android:id="@+id/LayoutFour"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/beige"
    android:orientation="horizontal" >

     <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x1.25" />

     <EditText

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>


     <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x1.50" />

     <EditText

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>


     <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x2" />

     <EditText

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1">
    </EditText>

  </LinearLayout>

【问题讨论】:

标签: java android android-layout


【解决方案1】:

您可以在按钮的onClick 中为 View 充气,如下所示:

addButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        LayoutInflater inflater = LayoutInflater
                        .from(getApplicationContext());
        View view = inflater.inflate(R.layout.job_row_layout, null);
        containerLayout.addView(view);

    }
});

希望这会有所帮助。

【讨论】:

  • 谢谢,我可以将我的作业布局 xml 从上面添加到布局文件夹中的新 xml 文件中吗??
  • 是的。 @bIG_aL 并膨胀该 xml。
  • 是否可以在创建每个视图时动态添加自定义 ID,以及如何构建我的代码来获取这些 ID?如果我想从创建的第 3 个或第 4 个工作中获取时间我怎么能这样做呢?我不知道我是否应该将此作为一个新问题发布,或者我应该在 cmets 中问这个抱歉
  • @bIG_aL 您也可以设置他们的 ID 并将其用于检索数据。 Example
  • 太好了,我要去研究你提供的例子,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2013-06-10
相关资源
最近更新 更多