【问题标题】:Alignment of dynamic views动态视图的对齐
【发布时间】:2017-12-05 07:33:09
【问题描述】:

我希望editext和imageview在同一水平线上对齐,但我得到edittext下方的imageview。

主要片段代码

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

newedittext 方法

  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 方法

 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

<?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,我的输出屏幕如下所示

我希望 imageviewedittext 在同一行中。谁能帮帮我。

当我添加“水平”时,这是输出

这是我的预期结果

【问题讨论】:

  • 您的lnrView 具有vertical 方向
  • 将 android:orientation="horizo​​ntal" 添加到您的内部视图
  • @ADM 没有得到预期的结果,我已经通过添加方向 = 水平来更新问题
  • @Shaishav 通过将方向更改为水平没有帮助,

标签: android android-layout android-fragments view android-xml


【解决方案1】:

您需要先在lnrView 中添加Horizontal Linearlayout,而不是添加EditTextImageview

试试这个

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

【讨论】:

  • 我已经使用水平方向更新了结果,这不是我想要的,
  • @AmruthaSaj 检查更新并在我的设备中正常工作
  • @AmruthaSaj 很乐意帮助您享受编码的乐趣
【解决方案2】:

或者更简单的使用

ListView

ArrayAdapter

在哪里定义自己的布局(EditTextbutton

有更好的方法来管理添加和删除行。

此外,您将所有EditTexts 放在一个数组中,您可以更简单地执行逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    相关资源
    最近更新 更多