我正在尝试将视图充气到约束布局中。这不是活动中的问题。但是我还希望能够从另一个名为getViews()
的类方法中调用它。如何访问约束布局以使视图膨胀?
public class chromecastUI_inflater {
Button btn, btn1, fastforward, rewind, play, pause;
TextView title, user, discription, date, streamviews, chromeconnection;
ConstraintLayout constraintLayout1;
Selector selector = new Selector();
Context c;
public chromecastUI_inflater() {
****编辑****
还尝试通过公共构造函数添加布局。
}
public void getViews(View view) {
play = view.findViewById(R.id.play);
pause = view.findViewById(R.id.pause_button);
fastforward = view.findViewById(R.id.fastforward_button);
rewind = view.findViewById(R.id.rewind_button);
btn1 = view.findViewById(R.id.cast_connection_button);
btn = view.findViewById(R.id.cast_connection_button3);
chromeconnection = view.findViewById(R.id.tvcast);
//here is my problem layout
****编辑**** 在这里我尝试添加
view.findViewById
constraintLayout1 = findViewById(R.id.localplayerview);
title = view.findViewById(R.id.title_text_view);
discription = view.findViewById(R.id.discription_text_view);
date = view.findViewById(R.id.date_text_view);
streamviews = view.findViewById(R.id.views_text_view);
user = view.findViewById(R.id.uploadedby_text_view);
constraintLayout1.addView(view);
}
public void inflateViews() {
LayoutInflater inflater = (LayoutInflater) c.getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout, null);
getViews(view);
}
public void setUI() {
title.setText(selector.DOCTITLE);
discription.setText(selector.ABOUT);
date.setText(selector.DATE);
streamviews.setText(selector.VIEWS);
user.setText(selector.USER);
}
public void resetUI() {
title.setText("");
discription.setText("");
date.setText("");
streamviews.setText("");
user.setText("");
}
public void Chromecast_Connected() {
chromeconnection.setText("Chromecast is connected");
btn.setVisibility(View.VISIBLE);
btn1.setVisibility(View.GONE);
}
public void Chromecast_Disconnected() {
chromeconnection.setText("Chromecast is connected");
btn.setVisibility(View.GONE);
btn1.setVisibility(View.VISIBLE);
}
}
这是我想从我的local_player
活动中调用方法的地方
chromecastUI_inflater chromecastUiInflater = new chromecastUI_inflater();
@Override
public void onChromecastConnecting() {
Log.d(getClass().getSimpleName(), "onChromecastConnecting");
chromecastUiInflater.inflateViews();
}
@Override
public void onChromecastConnected(ChromecastYouTubePlayerContext chromecastYouTubePlayerContext) {
Log.d(getClass().getSimpleName(), "onChromecastConnected");
chromecastUiInflater.setUI();
chromecastUiInflater.Chromecast_Connected();
initializeCastPlayer(chromecastYouTubePlayerContext);
}
对不起,我习惯于在活动内部夸大视图。请帮忙。
答案 0 :(得分:1)
所以这是我修复它的方法。我必须在我的inflate view方法中创建一个称为root view的视图。然后从我的活动中将rootview称为约束布局。
public void inflateViews(View rootview,Context c){
constraintLayout1 = rootview.findViewById(R.id.localplayerview);
LayoutInflater inflater = (LayoutInflater)c.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout,null);
getViews(view);
}
然后我必须制作一个getStrings方法以从膨胀视图填充文本视图
public void getStrings_fromLocalPlayer(String title,String synopsis, String user, String views, String date){
doctitle = title;
docsynopsis = synopsis;
docuser = user;
docviews = views;
docdate = date;
}