【问题标题】:Set Android Components Programmatically以编程方式设置 Android 组件
【发布时间】:2017-08-23 10:19:52
【问题描述】:

我有一个如下所示的 xml。

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<LinearLayout
    android:id="@+id/etMsisdn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/allView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/msisdn"
            android:layout_marginRight="4dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:hint="MSISDN"
            android:inputType="numberDecimal"
            />
        <ImageView
            android:layout_width="60px"
            android:layout_height="match_parent"
            android:src="@drawable/scan"/>
    </LinearLayout>
</LinearLayout>
............
Another View
............
</LinearLayout>

如何以编程方式在水平 LinearLayout (allView) 中添加 EditText 和 ImageView,并在 Vertical LinearLayout(etMsisdn) 中添加 allView,同时保持与 xml 中相同的属性。

EditText 和 ImageView r 应该在 msisdn 编辑文本下方

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    这是你的解决方案

    LinearLayout allview=(LinearLayout)findViewById(R.id.allView);
    
    EditText edt=new EditText(this);
    ImageView img=new ImageView(this);
    allview.addView(edt);
    allview.addView(img);
    

    把它放在你的活动中

    【讨论】:

    • 这样做会水平而不是垂直添加imgview和edittext
    • 你可以设置参数或改变方向
    【解决方案2】:

    您必须在布局对象上使用addView 方法。 但是,如果您想在主布局中添加更多次相同的“子布局”,最好使用“子部分”创建一个 xml 布局并以编程方式添加它。如果您需要提供代码,请告诉我第二种情况。

    【讨论】:

    • 我想在线性布局(主)中添加线性布局(子)。但是在子线性布局内部包含另一个视图,例如edittext和imageview
    【解决方案3】:

    找到LinerLayout并添加视图:

    LinearLayout root = (LinearLayout)findViewById(R.id.allView);
    root.addView(someView);
    

    并在您的视图中添加这样的属性:

    How to programmatically set textview-s and their properties?

    【讨论】:

      【解决方案4】:

      您需要获得对最外层LinearLayout(布局中的第一个)的引用,所以最好的办法是给它一个Id:

      <LinearLayout
          android:id="@+id/myContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="16dp">
      

      然后,您需要获取对此布局的引用并添加子视图,如下所示:

      LinearLayout containerLayout = (LinearLayout)findViewById(R.id.myContainer);
      containerLayout.addView(yourView1);
      containerLayout.addView(yourView2);
      

      要设置所需的布局对齐方式,您可以手动设置所需的LayoutParams(请参阅this answer in SO),或者您可以扩展布局并将其添加到当前布局,而不是两个单独的视图(EditTextImageView)(见this answer in SO)。

      【讨论】:

      • 已经这样做了,,,但无法让edittext和imageview水平对齐
      • 那是另一回事。您可以手动添加所需的 LayoutParams(请参阅 this answer in SO),也可以扩展布局(请参阅 this answer in SO)。
      • 我已经用之前的信息更新了答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2011-04-28
      • 2015-05-29
      • 2011-12-16
      • 2013-10-31
      • 1970-01-01
      • 2018-02-17
      相关资源
      最近更新 更多