Java 线性布局嵌套
是否可以创建一个方法,使布局膨胀(或动态创建),向其中添加特定视图(在本例中为TextViews),然后将布局作为视图重新运行,以便(以某种方式)将其合并到另一个类的主布局中,如嵌套,但动态添加元素?是,可以使用该类对现有布局进行充气。它会是这样的:Java 线性布局嵌套,java,android,view,Java,Android,View,是否可以创建一个方法,使布局膨胀(或动态创建),向其中添加特定视图(在本例中为TextViews),然后将布局作为视图重新运行,以便(以某种方式)将其合并到另一个类的主布局中,如嵌套,但动态添加元素?是,可以使用该类对现有布局进行充气。它会是这样的: public static View GetLayout(final Context c){ final LayoutInflater inflater = LayoutInflater.from(c); final View v
public static View GetLayout(final Context c){
final LayoutInflater inflater = LayoutInflater.from(c);
final View v = inflater.inflate(R.layout.activity_main, null);
//find the container where you want to insert your dynamic items
final LinearLayout placeholder = (LinearLayout) v.findViewById(R.id.linearLayout1);
//create the new textview
final TextView text = new TextView(c);
text.setText("Some text");
placeholder.addView(text, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return v;
}
这个问题要求将这个视图返回到“其他类”,其他答案来自同一个类,这意味着它有一个上下文。为了从另一个类执行此操作,您需要像我在这里所做的那样在上下文中传递,或者以其他方式传递(如果需要实例对象,则调用构造函数等)。是的,您可以使用该类扩展现有布局。它会是这样的:
public static View GetLayout(final Context c){
final LayoutInflater inflater = LayoutInflater.from(c);
final View v = inflater.inflate(R.layout.activity_main, null);
//find the container where you want to insert your dynamic items
final LinearLayout placeholder = (LinearLayout) v.findViewById(R.id.linearLayout1);
//create the new textview
final TextView text = new TextView(c);
text.setText("Some text");
placeholder.addView(text, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return v;
}
这个问题要求将这个视图返回到“其他类”,其他答案来自同一个类,这意味着它有一个上下文。为了从另一个类执行此操作,您需要像我在这里所做的那样在上下文中传递,或者以其他方式传递(如果需要实例对象,则调用构造函数等)。是的,可能:
... onCreate()
{
setContentView(...);
ViewGroup myContainer=(ViewGroup)findViewById(R.id.myContainer);
View v=inflateMySpecialView();
//=<set layoutParams for the view if needed
myContainer.addView(v);
}
public View inflateMySpecialView()
{
ViewGroup viewgroup=(ViewGroup ) getLayoutInflater().inflate(R.layout.my_custom_layout, null,false);
//do some stuff with the inflated viewgroup.
return viewgroup;
}
。。。onCreate()
{
setContentView(…);
ViewGroup myContainer=(ViewGroup)findViewById(R.id.myContainer);
视图v=充气专用视图();
//=是,这是可能的:
... onCreate()
{
setContentView(...);
ViewGroup myContainer=(ViewGroup)findViewById(R.id.myContainer);
View v=inflateMySpecialView();
//=<set layoutParams for the view if needed
myContainer.addView(v);
}
public View inflateMySpecialView()
{
ViewGroup viewgroup=(ViewGroup ) getLayoutInflater().inflate(R.layout.my_custom_layout, null,false);
//do some stuff with the inflated viewgroup.
return viewgroup;
}
…onCreate()
{
setContentView(…);
ViewGroup myContainer=(ViewGroup)findViewById(R.id.myContainer);
视图v=充气专用视图();
//=是的。我在我的应用程序中做了类似的事情。我正在编写一个抽认卡应用程序,我使用滚动视图向用户显示他们创建的所有卡片组。代码注释如下:
public void FillDeckView(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Deck testDeck = Deck.GetDeckFromDB();//Make a deck object from SQLite DB values
final int DECK_SIZE = 20;//Fill the view with this many decks
TableLayout scrollTable = (TableLayout) findViewById(R.id.scrollTable);
//This tableRow is in my main view
TableRow newTableRow = (TableRow) findViewById(R.id.tableRow4);
//This view I'm inflating is a separate android layout XML file
//I created, which shows the icon for the deck the user has made.
View deckViewBox = inflater.inflate(R.layout.deck_icons, null);
//Look, I'm getting a textview that's IN the deckViewBox, which is
//a separate View. Below, I'll change its text dynamically
TextView deckNameTV = (TextView) deckViewBox.findViewById(R.id.deckNameView);
int viewIndex = 1;
for(int i = 0; i < DECK_SIZE && testDeck != null; i++){
//First I'll change the text of the view, and then I'll add it in
deckNameTV.setText(testDeck.getName());
newTableRow.addView(deckViewBox);
if(i % 2 != 0){
//If we're on an odd number, make a new tableRow
newTableRow = new TableRow(getApplicationContext());
scrollTable.addView(newTableRow, viewIndex);
++viewIndex;
}
}
}//FillDeckView
public void FillDeckView(){
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
Deck testDeck=Deck.GetDeckFromDB();//从SQLite DB值生成Deck对象
final int DECK_SIZE=20;//用这么多的组填充视图
TableLayout scrollTable=(TableLayout)findViewById(R.id.scrollTable);
//这张桌子在我的主视图中
TableRow newTableRow=(TableRow)findViewById(R.id.tableRow4);
//我正在膨胀的这个视图是一个单独的android布局XML文件
//我创建了一个,它显示了用户制作的甲板的图标。
查看甲板ViewBox=充气机。充气(R.layout.deck_图标,空);
//看,我在deckViewBox中得到了一个文本视图,它是
//一个单独的视图。下面,我将动态更改其文本
TextView deckNameTV=(TextView)deckViewBox.findViewById(R.id.deckNameView);
int viewIndex=1;
对于(int i=0;i
基本上,您需要从一个新布局膨胀视图,然后调用findViewById()作为您创建的新视图的一种方法。从那里,它就像操纵用户可以看到的当前视图。您可以从那里做任何您想做的事情。是的。我在我的应用程序中也做了类似的事情。我正在编写一个抽认卡应用程序,我使用滚动视图向用户显示他们创建的所有组。代码注释如下:
public void FillDeckView(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Deck testDeck = Deck.GetDeckFromDB();//Make a deck object from SQLite DB values
final int DECK_SIZE = 20;//Fill the view with this many decks
TableLayout scrollTable = (TableLayout) findViewById(R.id.scrollTable);
//This tableRow is in my main view
TableRow newTableRow = (TableRow) findViewById(R.id.tableRow4);
//This view I'm inflating is a separate android layout XML file
//I created, which shows the icon for the deck the user has made.
View deckViewBox = inflater.inflate(R.layout.deck_icons, null);
//Look, I'm getting a textview that's IN the deckViewBox, which is
//a separate View. Below, I'll change its text dynamically
TextView deckNameTV = (TextView) deckViewBox.findViewById(R.id.deckNameView);
int viewIndex = 1;
for(int i = 0; i < DECK_SIZE && testDeck != null; i++){
//First I'll change the text of the view, and then I'll add it in
deckNameTV.setText(testDeck.getName());
newTableRow.addView(deckViewBox);
if(i % 2 != 0){
//If we're on an odd number, make a new tableRow
newTableRow = new TableRow(getApplicationContext());
scrollTable.addView(newTableRow, viewIndex);
++viewIndex;
}
}
}//FillDeckView
public void FillDeckView(){
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
Deck testDeck=Deck.GetDeckFromDB();//从SQLite DB值生成Deck对象
final int DECK_SIZE=20;//用这么多的组填充视图
TableLayout scrollTable=(TableLayout)findViewById(R.id.scrollTable);
//这张桌子在我的主视图中
TableRow newTableRow=(TableRow)findViewById(R.id.tableRow4);
//我正在膨胀的这个视图是一个单独的android布局XML文件
//我创建了一个,它显示了用户制作的甲板的图标。
查看甲板ViewBox=充气机。充气(R.layout.deck_图标,空);
//看,我在deckViewBox中得到了一个文本视图,它是
//一个单独的视图。下面,我将动态更改其文本
TextView deckNameTV=(TextView)deckViewBox.findViewById(R.id.deckNameView);
int viewIndex=1;
对于(int i=0;i
基本上,您需要从一个新布局膨胀视图,然后调用findViewById()作为您创建的新视图的一种方法。从那里,这就像操作用户可以看到的当前视图一样。您可以从那里执行任何操作。每个视图组都有自己的LayoutParams类。请确保导入了正确的布局参数(或在创建它们时指定包)--请记住,参数是为父级而不是要添加的项键入的。请将整个源粘贴到pastebin中。每个视图组都有自己的LayoutParams类。请确保导入了正确的布局参数(或指定t)