动态视图的对齐 [英] Alignment of dynamic views
问题描述
我希望editext和imageview在同一水平线上对齐,但是我将imageview移到了edittext下方.
I want the alignment of the editext and the imageviewin the same horizontal line but I am getting the imageview below the edittext.
主要片段代码
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方法
neweditext method
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方法
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;
}
主要片段xml
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,并且输出屏幕如下所示
when i click the Add button an edittext and Imageview is created dynamically and my output screen look like below
我希望imageview
和edittext
在同一行中.谁能帮助我.
i want the imageview
and the edittext
in the same line. can anyone help me.
当我添加水平"时,这是输出
when i add "Horizontal" this is the output
这是我的预期结果
推荐答案
您需要先在lnrView
中添加Horizontal
Linearlayout
,然后再添加EditText
和Imageview
You need add first a Horizontal
Linearlayout
in Your lnrView
than add EditText
and 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);
}
});
这篇关于动态视图的对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!