我正在开发 android studio
。我在如下片段中创建了一个线性布局:
<LinearLayout
android:id="@+id/ll_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_round"
android:orientation="vertical"
android:padding="5sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<AutoCompleteTextView
android:id="@+id/tv_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:gravity="left"
android:inputType="text"
android:hint = "Enter Product"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Enter Quantity"
android:gravity="left"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Prod Price"
android:gravity="left"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_specs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Prod Specs"
android:gravity="left"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:padding="0dp">
<Button
android:id="@+id/btn_prd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Product"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
图形界面
我想做什么?
点击 Add New Product
按钮后,我想重新创建相同的线性布局以及 textviews
。
在上图中,产品名称、价格和规范是从存储在用户手机中的 JSON
文件中取出的。
我尝试过的
下面是我尝试做的代码
addProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Add product button click " , Toast.LENGTH_SHORT).show();
LinearLayout linearLayoutProduct = new LinearLayout(getActivity());
linearLayoutProduct.findViewById(R.id.ll_out);//Stuck here
}
});
更新 1
我想让应用程序像下面这样
在上图中,当我单击 plus
符号时,会使用 cross
按钮等创建新行。我想要一模一样
我该怎么做?
如有任何帮助,我们将不胜感激。
最佳答案
为此,您需要两个单独的布局,一个是父布局,另一个是子布局。在父 View 中,只有 LinearLayout,而另一个 View 将包含一个自定义布局,您可以在此添加自定义布局。
例如,请遵循此。
layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
还有一些像这样的布局:
layout2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1"/>
</LinearLayout>
您可以扩充 layout2.xml
文件,编辑文本,并将其添加到第一个布局:
public class MyFragment extends Fragment {
private LinearLayout mLinearLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourMainLayout, container, false);
.
.
.
mLinearLayout = (LinearLayout) view.findViewById(R.id.linear_layout);
Button fab = (Button) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// new elements on click
addLayout("This is text 1", "This is first button", "This is second Button");
}
});
addLayout("This is text 1", "This is first button", "This is second Button");
}
private void addLayout(String textViewText, String buttonText1, String buttonText2) {
View layout2 = LayoutInflater.from(getActivity()).inflate(R.layout.layout2, mLinearLayout, false);
TextView textView = (TextView) layout2.findViewById(R.id.button1);
Button button1 = (Button) layout2.findViewById(R.id.button2);
Button button2 = (Button) layout2.findViewById(R.id.button3);
textView1.setText(textViewText);
button1.setText(buttonText1);
button2.setText(buttonText2);
mLinearLayout.addView(layout2);
}
您可能想要将 layout2.xml
Root View 的 android:layout_height
更改为 wrap_content
。
这里我没有把Button放在XML LAYOUT中,大家可以根据需要放Button。
如果您正在使用 ViewBinding,下面是 addLayout
函数的样子:
MyLayoutBinding binding = MyLayoutBinding.inflate(getLayoutInflater(), mLinearLayout, false);
binding.getTextView1().setText(textViewText);
binding.getButton1().setText(buttonText1);
binding.getButton2().setText(buttonText2);
mLinearLayout.addView(binding.getRoot());
在您的案例中,您可以从添加按钮的 onClick() 调用此方法
addLayout("This is text 1", "This is first button", "This is second Button");
关于java - 在 android studio 中单击按钮时创建线性布局可编程性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73090637/