我有Custome List视图,其中包含几个textview和imageview。我想得到那个视图。我怎么能得到它。我的代码如下。我想在整个aap.ts工作正常的应用字体样式查看bt不在列表视图的适配器中。请建议我。
public class BaseFragment extends Fragment {
private Typeface typeace;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
typeace = Typeface.createFromAsset(getActivity().getAssets(), getResources().getString(R.string.font_name_fragment));
//FlurryAgent.init(getActivity(), getResources().getString(R.string.flurry_key));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
overrideFonts(getActivity(), view);
}
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView) {
((TextView) v).setTypeface(typeace);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(typeace);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您可以按如下方式获取ListView中的列表项,并为其添加自己的字体
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView txtView = (TextView) view.findViewById(R.id.txt_view);
txtView.setTypeface(typeace);
}
});
或在适配器中也作为
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = null;
if (view == null) {
view = layoutInflater.inflate(R.layout.child_layout, parent, false);
holder = new ViewHolder();
holder.txt = (TextView) view.findViewById(R.id.txt_view);
holder.txt.setTypeFace(typeface);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt.setText("Text");
return view;
}