Android 如何使用for循环以编程方式创建LinearLayout
我正试图从Android 如何使用for循环以编程方式创建LinearLayout,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我正试图从数组中包含的信息以编程方式创建线性布局 让我解释一下: 我创建的专业应用程序将不在PlayStore上 我有以下手动填充的数组(一种全局变量): 对于某些客户,它只包含一个条目: static final String[][] reseaux = new String[][] { {"Net1","192.168.0.1"} }; 根据只包含一个条目的数组,我创建了以下布局: <?xml version="1.0" encoding="utf-
数组中包含的信息以编程方式创建线性布局
让我解释一下:
我创建的专业应用程序将不在PlayStore上
我有以下手动填充的数组(一种全局变量):
对于某些客户,它只包含一个条目:
static final String[][] reseaux = new String[][] {
{"Net1","192.168.0.1"}
};
根据只包含一个条目的数组
,我创建了以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcome"
android:layout_gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="Overdraw">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/linearLayoutTest"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
以及守则:
LinearLayout l = findViewById(R.id.linearLayoutTest);
for (int i = 0; i < reseaux.length; ++i) {
final int id = i;
ImageButton resBtn = new ImageButton(this);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setText(reseaux[i][0]);
resBtn.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.button_reseaux));
resBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
l.addView(resBtn);
l.addView(tv);
resBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Check network
if (CheckNetworkStatus.isNetworkAvailable(getApplicationContext())) {
Intent i = new Intent(getApplicationContext(),
HomeReseauActivity.class);
startActivity(i);
BASE_URL = null;
nomReseau = null;
BASE_URL = reseaux[id][1];
nomReseau = reseaux[id][0];
} else {
//Error message if no network connexion
if (toastMessage != null) toastMessage.cancel();
toastMessage = Toast.makeText(HomeActivity.this, getString(R.string.networkError), Toast.LENGTH_LONG);
toastMessage.show();
}
}
});
LinearLayout l=findviewbyd(R.id.LinearLayout测试);
对于(int i=0;i
请给出以下最适合我的结果:
现在,当我的数组中有多个条目时,我得到的是:
这不适合我。我喜欢三个图标的水平线。当一行被填充时,它会创建一个三个图标的新行,等等
以下是使用paint进行的快速编辑,以演示我想要的内容:
我认为解决办法一定很简单,但我不能得到这个结果
纵容我的代码,我自己学习所有东西,当然还有更好的写作方法:-)
希望有人能帮助我!正如我的问题评论中所述,解决方案是使用RecyclerView和GridLayoutManager
对于未来的读者来说,这本书对我帮助很大
感谢所有回答我问题的人。使用GridView
。最好是使用GridLayoutManager的RecyclerView。同时使用RecyclerView的更好方法是使用GridLayoutManager的RecyclerView
LinearLayout l = findViewById(R.id.linearLayoutTest);
for (int i = 0; i < reseaux.length; ++i) {
final int id = i;
ImageButton resBtn = new ImageButton(this);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setText(reseaux[i][0]);
resBtn.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.button_reseaux));
resBtn.setBackgroundColor(getResources().getColor(R.color.transparent));
l.addView(resBtn);
l.addView(tv);
resBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Check network
if (CheckNetworkStatus.isNetworkAvailable(getApplicationContext())) {
Intent i = new Intent(getApplicationContext(),
HomeReseauActivity.class);
startActivity(i);
BASE_URL = null;
nomReseau = null;
BASE_URL = reseaux[id][1];
nomReseau = reseaux[id][0];
} else {
//Error message if no network connexion
if (toastMessage != null) toastMessage.cancel();
toastMessage = Toast.makeText(HomeActivity.this, getString(R.string.networkError), Toast.LENGTH_LONG);
toastMessage.show();
}
}
});