Android 如何将cardview动态添加到网格布局
我正在尝试将cardview动态添加到GridLayout。但它并没有添加cardview 层次结构是ScrollView-->水平ScrollView-->线性布局-->GridLayout,我想添加到此网格布局。我的代码是:Android 如何将cardview动态添加到网格布局,android,android-layout,grid-layout,Android,Android Layout,Grid Layout,我正在尝试将cardview动态添加到GridLayout。但它并没有添加cardview 层次结构是ScrollView-->水平ScrollView-->线性布局-->GridLayout,我想添加到此网格布局。我的代码是: public class MainActivity extends AppCompatActivity { GridLayout gridLayout; TextView tc; CardView newc; @SuppressLint("SetTextI18n") @
public class MainActivity extends AppCompatActivity {
GridLayout gridLayout;
TextView tc;
CardView newc;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = findViewById(R.id.gridlayout);
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
tc.setText("This is dynamic");
newc.addView(tc);
GridLayout.Spec row = GridLayout.spec(0);
GridLayout.Spec col = GridLayout.spec(0);
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
gridLayout.addView(newc, gridP);
}
}
因此,如何将视图动态添加到GridLayout
谢谢。您的代码中有一些错误。。。用这个
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tc.setText("This is dynamic");
newc.addView(tc);
gridLayout.setColumnCount(2);
gridLayout.setOrientation(GridLayout.HORIZONTAL);
gridLayout.addView(newc);
GridLayout.Spec row = GridLayout.spec(0); // Mention row index here
GridLayout.Spec col = GridLayout.spec(0);// Mention column index here
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
如果要将视图添加到特定行和特定列,请使用此
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tc.setText("This is dynamic");
newc.addView(tc);
gridLayout.setColumnCount(2);
gridLayout.setOrientation(GridLayout.HORIZONTAL);
gridLayout.addView(newc);
GridLayout.Spec row = GridLayout.spec(0); // Mention row index here
GridLayout.Spec col = GridLayout.spec(0);// Mention column index here
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
在您的情况下,行和列总是使用0,0,所以基本上发生的是视图被添加到网格布局中,但它们是重叠的,因此您无法看到这些视图。代码中有一些错误。。。用这个
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tc.setText("This is dynamic");
newc.addView(tc);
gridLayout.setColumnCount(2);
gridLayout.setOrientation(GridLayout.HORIZONTAL);
gridLayout.addView(newc);
GridLayout.Spec row = GridLayout.spec(0); // Mention row index here
GridLayout.Spec col = GridLayout.spec(0);// Mention column index here
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
如果要将视图添加到特定行和特定列,请使用此
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tc.setText("This is dynamic");
newc.addView(tc);
gridLayout.setColumnCount(2);
gridLayout.setOrientation(GridLayout.HORIZONTAL);
gridLayout.addView(newc);
GridLayout.Spec row = GridLayout.spec(0); // Mention row index here
GridLayout.Spec col = GridLayout.spec(0);// Mention column index here
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
在您的例子中,行和列总是使用0,0,所以基本上发生的是视图被添加到网格布局中,但它们是重叠的,因此您无法看到这些视图