Use single xml layout for multiple activities with different datas

前端 未结 2 1724
轻奢々
轻奢々 2020-12-19 01:21

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

2条回答
  •  隐瞒了意图╮
    2020-12-19 02:11

    Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.same_layout);
    
        TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer);
        urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2");
    
    
        ImageButton aButton = (ImageButton)findViewById(R.id.videolink);
        aButton.setOnClickListener(aButtonListener);
    }
    
    private OnClickListener aButtonListener = new OnClickListener() {
        public void onClick(View v) {
            // go open url_1 here. In other activities, open url_x, url_y, url_z
            finish();
        }
    };
    

    Same code just swapping the text you want to set for the TextView and url to open in OnClickListener(). No more to change.

提交回复
热议问题