android – 动态加载XML文件到Linearlayout

我有2个像这样的xml文件

main.xml中:

<horizontalscrollview
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    </LinearLayout>
</horizontalscrollview>

items.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="100dp"
android:layout_height="100dp">

   <ImageView
    android:id="@+id/img"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:contentDescription="@string/app_name"
    android:gravity="center"
    android:src="@drawable/icon_shop2" />

   <TextView
    android:id="@+id/providerName"
    style="@style/WhiteTextMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img"
    android:layout_gravity="bottom"
    android:paddingLeft="10dp"
    android:text="Example" />

</RelativeLayout>

main.xml用于MainActivity.
我的问题是如何在main.xml的Horizo​​ntalScrollView中动态加载3个布局items.xml到Linearlayout!

解决方法

试试这种方式,希望这可以帮助您解决问题.
LinearLayout ll;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   ll = (LinearLayout) findViewById(R.id.ll);
   for (int i=0;i<3;i++){
        View view = LayoutInflater.from(this).inflate(R.layout.items,null);
        ImageView imageView =  view.findViewById(R.id.img);
        TextView providerName =  view.findViewById(R.id.providerName);
        // Assigning value to  imageview and textview here
        ll.addView(view);
}

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...