简体   繁体   中英

Best way to create a lot of new layouts

I'm currently making a local news app and the main layout has 10 image buttons (more to be added in the future) and I was wondering what would be the best way to get each of these to open a separate layout with an individual text view without making 10 seperate classes and maybe even without making 10 separate layouts. Right now my MainActivty class handles the first button from the layout main_activity which opens a new layout named issue.XML.

Thanks in advanced.

You can create a layout called "newsLayout" that has just one text view and sets it from intent and make the onClick listener of the buttons to start a new activity and passing the desired text (the news) to the intent

Here is some code to help:

newsLayout.java

TextView text = findViewById(R.id.text);
text.setText(getIntent().getCharArrayExtra("TEXT"));

mainLayout.java

public void openNews(View view)
{
    Intent intent = new Intent(this, newsLayout.class);
     intent.putExtra("TEXT", newsText);
     startActivity(intent);
}

Where 'newsText' is the text you want to be shown

I may have wrote some lines wrong because im answering from mobile and i dont remember the exact words, if there's anything you dont understand tell me :)

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