简体   繁体   中英

LinearLayout nesting

是否有可能创建一种方法来对布局进行充气(或动态创建),向其添加特定视图(在这种情况下为TextViews),然后将布局重新调整为视图,因此我可以(以某种方式)将其合并到主视图中在另一个类中进行布局,例如嵌套,但是要动态添加元素?

Yes, you can use the LayoutInflater class to inflate an existing layout. It would go something like:

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;

}

The question asked that this view be returned to the "other class", and the other answers are doing so from the same class -- which means it has a context. In order to do so from another class you need to pass in a context like I'm doing here, or some other way (constructor call if you want an instance object, etc).

yes it is possible:

... 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;
  }

Yes, it is. I do a similar thing in my app. I'm writing a flashcard app, and I use a scrollview to show the user all the decks they have created. The code is commented:

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

Basically, you need to inflate the view from a new layout and then call findViewById() as a method of the new View you've created. From there, it's just like manipulating the current view the user can see. You can do anything you want from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM