Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - Use single xml layout for multiple activities with different datas

I know this is a very basic question, however as a newbie i cant get to work around it. So, I want to have multiple activities to use same the xml layout(consist for example of 1 imagebutton, and multiple textviews with different IDs). Now, for every activity, I want them to view the same layout but override the views with data unique to every activity. What is the best way to do this? And also, the imagebutton should open different URLs in a video player(youtube links).

And can somebody tell me what is the most practical way to learn android programming?

UPDATE This is my current code:

public class TemakiActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contentviewer);
}

}

For example I have a textview with ID "descriptionviewer", and a button with ID "videolink", now, how do you code those in?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can share the same layout file and the set the attributes for views in the onCreate(..) method of each activity.

If you want a different URL to open for each image button you could set it at runtime as follows

public void onCreate(Bundle b) {

    Button button =(Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            //different action for each activity
        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...