LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.your_layout, null); // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.a_text_view); textView.setText("your text"); // insert into main view ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point); insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); View something = rootView.findViewById(R.id.something); // not activity findViewById() something.setOnClickListener(new View.OnClickListener() { ... }); return rootView; }
final View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); if (heightDiff > dpToPx(this, 200)) { // if more than 200 dp, it's probably a keyboard... // ... do something here } } });
public void onAnimationEnd(Animator animation) { dropView.setVisibility(GONE); dropView.setAlpha(1); } });
View positiveButton = findViewById(R.id.positiveButton); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)positiveButton.getLayoutParams(); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); positiveButton.setLayoutParams(layoutParams);
AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = getApplicationContext(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create();
if (view == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.my_custom_list_layout, null); TextView listItemText = (TextView)view.findViewById(R.id.list_item_string); listItemText.setText(list.get(position)); Button deleteBtn = (Button)view.findViewById(R.id.delete_btn); Button addBtn = (Button)view.findViewById(R.id.add_btn); deleteBtn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { addBtn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout..., container, false); Button button = view.findViewById(R.id...); button.setOnClickListener(...); return view; }
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frag_simple_line, container, false); chart = v.findViewById(R.id.lineChart1); chart.getDescription().setEnabled(false); chart.setDrawGridBackground(false); chart.setData(getComplexity()); chart.animateX(3000); Typeface tf = Typeface.createFromAsset(context.getAssets(), "OpenSans-Light.ttf"); Legend l = chart.getLegend(); l.setTypeface(tf); YAxis leftAxis = chart.getAxisLeft(); leftAxis.setTypeface(tf); chart.getAxisRight().setEnabled(false); XAxis xAxis = chart.getXAxis(); xAxis.setEnabled(false); return v; } }
public void showNetworkError(View.OnClickListener onClickListener) { View layout = helper.inflate(R.layout.message); TextView textView = (TextView) layout.findViewById(R.id.message_info); textView.setText(helper.getContext().getResources().getString(R.string.common_no_network_msg)); ImageView imageView = (ImageView) layout.findViewById(R.id.message_icon); imageView.setImageResource(R.drawable.ic_exception); if (null != onClickListener) { layout.setOnClickListener(onClickListener); } helper.showLayout(layout); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View result=inflater.inflate(R.layout.main, container, false); btn=(Button)result.findViewById(R.id.go); btn.setOnClickListener(this); btn.setEnabled(binding!=null); return(result); }
public View getView(int position, View convertView, ViewGroup parent) { View v; if (convertView == null) { // if it's not recycled, initialize some attributes LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); v = inflater.inflate(R.layout.gridview_item_layout, parent, false); } else { v = (View) convertView; } TextView text = (TextView)v.findViewById(R.id.grid_item_text); text.setText(mTextIds[position]); ImageView image = (ImageView)v.findViewById(R.id.grid_item_image); image.setImageDrawable(mThumbIds[position]); return v; }
@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); btnShow = (Button) mView.findViewById(R.id.btn_show); btnShow.setOnClickListener(this); mFrameLayout = (FrameLayout) mView.findViewById(R.id.fragmen_fragment); initTimePicker(); }
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.testclassfragment, container, false); ImageView imageView = (ImageView) view.findViewById(R.id.my_image); return view; }
if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.row, null); RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton); TextView tv = (TextView)v.findViewById(R.id.textview); tv.setText("Text view #" + position); RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton); r.setChecked(position == selectedPosition); r.setTag(position); r.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { selectedPosition = (Integer)view.getTag(); notifyDataSetChanged();
public void initList(List<SwitchVideoModel> data, OnListItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; this.data = data; LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.switch_video_dialog, null); listView = (ListView) view.findViewById(R.id.switch_dialog_list); setContentView(view); adapter = new ArrayAdapter<>(mContext, R.layout.switch_video_dialog_item, data); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener()); Window dialogWindow = getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用 lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6 dialogWindow.setAttributes(lp); }
private void bindView(int position, View row) { TextView label=(TextView)row.findViewById(R.id.label); label.setText(getItem(position).loadLabel(pm)); ImageView icon=(ImageView)row.findViewById(R.id.icon); icon.setImageDrawable(getItem(position).loadIcon(pm)); } }
private void createHeadView() { View headView = LayoutInflater.from(this).inflate(R.layout.layout_vip_game_head_view, mRecycle, false); ImageView mVipGameImage = (ImageView) headView.findViewById(R.id.vip_game_image); Glide.with(GameCentreActivity.this).load(mVipGameInfoData.getImgPath()) .diskCacheStrategy(DiskCacheStrategy.ALL).into(mVipGameImage); mVipGameImage.setOnClickListener(v -> BrowserActivity.launch(GameCentreActivity.this, mVipGameInfoData.getLink(), "年度大会员游戏礼包专区")); mHeaderViewRecyclerAdapter.addHeaderView(headView); } }
private View addItemView(final String title, @DrawableRes final int icon, ViewGroup container) { final View itemRoot = View.inflate(getContext(), R.layout.subscription_import_export_item, null); final TextView titleView = itemRoot.findViewById(android.R.id.text1); final ImageView iconView = itemRoot.findViewById(android.R.id.icon1); titleView.setText(title); iconView.setImageResource(icon); container.addView(itemRoot); return itemRoot; }