gpt4 book ai didi

android - 将 XML 文件动态加载到 Linearlayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:28 24 4
gpt4 key购买 nike

我有 2 个这样的 xml 文件:

ma​​in.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。我的问题是如何在 ma​​in.xmlHorizo​​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 - 将 XML 文件动态加载到 Linearlayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com