gpt4 book ai didi

java - 自动设置内容

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:31 24 4
gpt4 key购买 nike

我是 Android Studio 的新手,我有一个问题。我目前正在使用此方法来 SetContent

 ImageView img1 = (ImageView)findViewBtId(R.id.Img1);
ImageView img2 = (ImageView)findViewBtId(R.id.Img2);`

但我想排列 img1,img2,img3所以我可以自动设置内容,像这样

 ArrayList <ImageView> imgs = new ArrayList <ImageView>();
imgs.add("img1");
imgs.add("img2");
imgs.add("img3");

for (int i =0;int i < imgs.length();i++)
{ imgs[i] = ...;}

我可以使用这个还是其他东西? Android Studio 不能有这样的东西吗?

最佳答案

You can assign dynemically like: 

Here is the 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" >

<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

And the Actvity File

public class TestActivity extends Activity{

private String[] id;
private ImageView[] imageViews = new ImageView[2];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testactivity);

int temp;
id = new String[]{"img1", "img2"};

for(int i=0; i<id.length; i++){
temp = getResources().getIdentifier(id[i], "id", getPackageName());
imageViews[i] = (ImageView) findViewById(temp);
}
}

Hope you understand and do according your need.

关于java - 自动设置内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58907650/

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