动态视图的对齐
Alignment of dynamic views
我希望编辑文本和图像视图在同一水平线上对齐,但我得到的图像视图低于编辑文本。
main fragment code
final LinearLayout lnrView = (LinearLayout)
child.findViewById(R.id.lnrView);
Button btnad = (Button) child.findViewById(R.id.btnad);
btnad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lnrView.addView(newedittext());
lnrView.addView(newImageview(getActivity()));
}
});
neweditext 方法
private View newedittext() {
final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(getActivity());
int id = 0;
editText.setId(id);
editText.setTextSize(14);
editText.setTextColor(Color.rgb(0, 0, 0));
editText.setMaxEms(2);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
return editText;
}
newImageview method
public ImageView newImageview(Context context) {
final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final ImageView imgView = new ImageView(context);
int id = 0;
imgView.setId(id);
imgView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_close_red));
return imgView;
}
main fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<LinearLayout
android:id="@+id/lnrView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
当我单击“添加”按钮时,会动态创建一个 edittext 和 Imageview,我的输出屏幕如下所示
我希望 imageview
和 edittext
在同一行。谁能帮帮我
当我添加 "Horizontal" 时,这是输出
这是我的预期结果
您需要先在 lnrView
中添加 Horizontal
Linearlayout
,然后再添加 EditText
和 Imageview
试试这个
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
layout.addView(newedittext());
layout.addView(newImageview(MainActivity.this));
lnrView.addView(layout);
}
});
或者更简单的使用
ListView
和
ArrayAdapter
在何处定义自己的布局(EditText
和 button
)
有更好的方法来管理添加和删除行。
此外,您将所有 EditTexts
都放在一个数组中,这样您的逻辑就更简单了。
我希望编辑文本和图像视图在同一水平线上对齐,但我得到的图像视图低于编辑文本。
main fragment code
final LinearLayout lnrView = (LinearLayout)
child.findViewById(R.id.lnrView);
Button btnad = (Button) child.findViewById(R.id.btnad);
btnad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lnrView.addView(newedittext());
lnrView.addView(newImageview(getActivity()));
}
});
neweditext 方法
private View newedittext() {
final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(getActivity());
int id = 0;
editText.setId(id);
editText.setTextSize(14);
editText.setTextColor(Color.rgb(0, 0, 0));
editText.setMaxEms(2);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
return editText;
}
newImageview method
public ImageView newImageview(Context context) {
final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final ImageView imgView = new ImageView(context);
int id = 0;
imgView.setId(id);
imgView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_close_red));
return imgView;
}
main fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<LinearLayout
android:id="@+id/lnrView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
当我单击“添加”按钮时,会动态创建一个 edittext 和 Imageview,我的输出屏幕如下所示
我希望 imageview
和 edittext
在同一行。谁能帮帮我
当我添加 "Horizontal" 时,这是输出
这是我的预期结果
您需要先在 lnrView
中添加 Horizontal
Linearlayout
,然后再添加 EditText
和 Imageview
试试这个
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
layout.addView(newedittext());
layout.addView(newImageview(MainActivity.this));
lnrView.addView(layout);
}
});
或者更简单的使用
ListView
和
ArrayAdapter
在何处定义自己的布局(EditText
和 button
)
有更好的方法来管理添加和删除行。
此外,您将所有 EditTexts
都放在一个数组中,这样您的逻辑就更简单了。