下面列出了android.widget.HorizontalScrollView#addView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public NavitationScrollLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
margleft = dip2px(context, 0);
titleLayout = new LinearLayout(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
titleLayout.setLayoutParams(layoutParams);
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
titleLayout.setGravity(Gravity.CENTER_VERTICAL);
LayoutParams layoutParams2 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
horizontalScrollView = new HorizontalScrollView(context);
horizontalScrollView.addView(titleLayout, layoutParams2);
horizontalScrollView.setHorizontalScrollBarEnabled(false);
addView(horizontalScrollView);
}
@Override
protected View getLayoutView() {
HorizontalScrollView scrollView = new HorizontalScrollView(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
scrollView.setFillViewport(true);
recyclerView = new MenuRecyclerView(getContext());
recyclerView.setBackgroundColor(ViewKnife.getColor(R.color.pd_main_bg));
scrollView.addView(recyclerView, params);
return scrollView;
}
private LinearLayout getPlatformList() {
LinearLayout llToolBar = new LinearLayout(getContext());
LinearLayout.LayoutParams lpTb = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llToolBar.setLayoutParams(lpTb);
TextView tvShareTo = new TextView(getContext());
int resId = getStringRes(activity, "share_to");
if (resId > 0) {
tvShareTo.setText(resId);
}
tvShareTo.setTextColor(0xffcfcfcf);
tvShareTo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
int dp_9 = dipToPx(getContext(), 9);
LinearLayout.LayoutParams lpShareTo = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpShareTo.gravity = Gravity.CENTER_VERTICAL;
lpShareTo.setMargins(dp_9, 0, 0, 0);
tvShareTo.setLayoutParams(lpShareTo);
llToolBar.addView(tvShareTo);
HorizontalScrollView sv = new HorizontalScrollView(getContext());
sv.setHorizontalScrollBarEnabled(false);
sv.setHorizontalFadingEdgeEnabled(false);
LinearLayout.LayoutParams lpSv = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSv.setMargins(dp_9, dp_9, dp_9, dp_9);
sv.setLayoutParams(lpSv);
llToolBar.addView(sv);
llPlat = new LinearLayout(getContext());
llPlat.setLayoutParams(new HorizontalScrollView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
sv.addView(llPlat);
return llToolBar;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mRootLayout = new HorizontalScrollView(getContext());
mRootLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
mParentLayout = new LinearLayout(getContext());
mParentLayout.setOrientation(LinearLayout.HORIZONTAL);
mScroller = new Scroller(mRootLayout.getContext(), new DecelerateInterpolator(1.2f));
mParentLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
mRootLayout.addView(mParentLayout);
addView(mRootLayout);
SetColumnSnap(true);
}
private LinearLayout getPlatformList() {
LinearLayout llToolBar = new LinearLayout(getContext());
LayoutParams lpTb = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llToolBar.setLayoutParams(lpTb);
TextView tvShareTo = new TextView(getContext());
int resId = getStringRes(activity, "share_to");
if (resId > 0) {
tvShareTo.setText(resId);
}
tvShareTo.setTextColor(0xffcfcfcf);
tvShareTo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
int dp_9 = dipToPx(getContext(), 9);
LayoutParams lpShareTo = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpShareTo.gravity = Gravity.CENTER_VERTICAL;
lpShareTo.setMargins(dp_9, 0, 0, 0);
tvShareTo.setLayoutParams(lpShareTo);
llToolBar.addView(tvShareTo);
HorizontalScrollView sv = new HorizontalScrollView(getContext());
sv.setHorizontalScrollBarEnabled(false);
sv.setHorizontalFadingEdgeEnabled(false);
LayoutParams lpSv = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSv.setMargins(dp_9, dp_9, dp_9, dp_9);
sv.setLayoutParams(lpSv);
llToolBar.addView(sv);
llPlat = new LinearLayout(getContext());
llPlat.setLayoutParams(new HorizontalScrollView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
sv.addView(llPlat);
return llToolBar;
}
private LinearLayout getPlatformList() {
LinearLayout llToolBar = new LinearLayout(getContext());
LayoutParams lpTb = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llToolBar.setLayoutParams(lpTb);
TextView tvShareTo = new TextView(getContext());
int resId = getStringRes(activity, "share_to");
if (resId > 0) {
tvShareTo.setText(resId);
}
tvShareTo.setTextColor(0xffcfcfcf);
tvShareTo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
int dp_9 = dipToPx(getContext(), 9);
LayoutParams lpShareTo = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpShareTo.gravity = Gravity.CENTER_VERTICAL;
lpShareTo.setMargins(dp_9, 0, 0, 0);
tvShareTo.setLayoutParams(lpShareTo);
llToolBar.addView(tvShareTo);
HorizontalScrollView sv = new HorizontalScrollView(getContext());
sv.setHorizontalScrollBarEnabled(false);
sv.setHorizontalFadingEdgeEnabled(false);
LayoutParams lpSv = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSv.setMargins(dp_9, dp_9, dp_9, dp_9);
sv.setLayoutParams(lpSv);
llToolBar.addView(sv);
llPlat = new LinearLayout(getContext());
llPlat.setLayoutParams(new HorizontalScrollView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
sv.addView(llPlat);
return llToolBar;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
textSize = (int) getResources().getDimension(R.dimen.cell_text_size);
padding = (int) getResources().getDimension(R.dimen.cell_padding);
Nosey nosey = Nosey.getInstance(this);
Inspector.ModelMapInspector inspector = new Inspector.ModelMapInspector();
inspector.inspect(nosey);
String modelKey = getIntent().getStringExtra(EXTRA_MODEL_KEY);
model = inspector.getModelMap().get(modelKey);
methods = model.getDeclaredMethods();
Arrays.sort(methods, new MemberComparator<Method>());
TableLayout table = new TableLayout(this);
addModelFieldHeaders(table, inspector);
addModelDataRows(table, inspector);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
scrollView.setHorizontalScrollBarEnabled(true);
scrollView.setVerticalScrollBarEnabled(true);
scrollView.setFillViewport(true);
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
horizontalScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
horizontalScrollView.setHorizontalScrollBarEnabled(true);
horizontalScrollView.setVerticalScrollBarEnabled(true);
horizontalScrollView.setFillViewport(true);
scrollView.addView(horizontalScrollView);
horizontalScrollView.addView(table);
setContentView(scrollView);
}
/************************************************************
**** Test code ****
************************************************************/
private void initCustomHeader() {
if (WelcomeActivity.demoType != DemoConfig.DemoType.Pull_to_add_Magic_Header_Mixed_Complicated_header) {
// Simply add a picture
addRandomPic();
} else {
// Example: add a custom Layout
View customLayout = LayoutInflater.from(this).inflate(R.layout.header_custom_layout, null);
Button btn = ((Button) customLayout.findViewById(R.id.button));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DemoActivity.this, "btn Clicked", Toast.LENGTH_SHORT).show();
}
});
mMagicHeaderViewPager.addHeaderView(customLayout);
HorizontalScrollView sv = new HorizontalScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 5; i > 0; i--) {
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(RandomPic.getInstance().getPicResId());
ll.addView(iv, new LinearLayout.LayoutParams(MagicHeaderUtils.getScreenWidth(this), (int) (MagicHeaderUtils.getScreenWidth(this)*.66f)));
}
sv.addView(ll, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mMagicHeaderViewPager.addHeaderView(sv);
}
}
private LinearLayout getPlatformList() {
LinearLayout llToolBar = new LinearLayout(getContext());
LinearLayout.LayoutParams lpTb = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llToolBar.setLayoutParams(lpTb);
TextView tvShareTo = new TextView(getContext());
int resId = getStringRes(activity, "share_to");
if (resId > 0) {
tvShareTo.setText(resId);
}
tvShareTo.setTextColor(0xffcfcfcf);
tvShareTo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
int dp_9 = dipToPx(getContext(), 9);
LinearLayout.LayoutParams lpShareTo = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpShareTo.gravity = Gravity.CENTER_VERTICAL;
lpShareTo.setMargins(dp_9, 0, 0, 0);
tvShareTo.setLayoutParams(lpShareTo);
llToolBar.addView(tvShareTo);
HorizontalScrollView sv = new HorizontalScrollView(getContext());
sv.setHorizontalScrollBarEnabled(false);
sv.setHorizontalFadingEdgeEnabled(false);
LinearLayout.LayoutParams lpSv = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSv.setMargins(dp_9, dp_9, dp_9, dp_9);
sv.setLayoutParams(lpSv);
llToolBar.addView(sv);
llPlat = new LinearLayout(getContext());
llPlat.setLayoutParams(new HorizontalScrollView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
sv.addView(llPlat);
return llToolBar;
}
private LinearLayout getPlatformList() {
LinearLayout llToolBar = new LinearLayout(getContext());
LinearLayout.LayoutParams lpTb = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llToolBar.setLayoutParams(lpTb);
TextView tvShareTo = new TextView(getContext());
int resId = getStringRes(activity, "share_to");
if (resId > 0) {
tvShareTo.setText(resId);
}
tvShareTo.setTextColor(0xffcfcfcf);
tvShareTo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
int dp_9 = dipToPx(getContext(), 9);
LinearLayout.LayoutParams lpShareTo = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpShareTo.gravity = Gravity.CENTER_VERTICAL;
lpShareTo.setMargins(dp_9, 0, 0, 0);
tvShareTo.setLayoutParams(lpShareTo);
llToolBar.addView(tvShareTo);
HorizontalScrollView sv = new HorizontalScrollView(getContext());
sv.setHorizontalScrollBarEnabled(false);
sv.setHorizontalFadingEdgeEnabled(false);
LinearLayout.LayoutParams lpSv = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSv.setMargins(dp_9, dp_9, dp_9, dp_9);
sv.setLayoutParams(lpSv);
llToolBar.addView(sv);
llPlat = new LinearLayout(getContext());
llPlat.setLayoutParams(new HorizontalScrollView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
sv.addView(llPlat);
return llToolBar;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
textSize = (int) getResources().getDimension(R.dimen.cell_text_size);
padding = (int) getResources().getDimension(R.dimen.cell_padding);
Nosey nosey = Nosey.getInstance(this);
Inspector.ModelMapInspector inspector = new Inspector.ModelMapInspector();
inspector.inspect(nosey);
String modelKey = getIntent().getStringExtra(EXTRA_MODEL_KEY);
model = inspector.getModelMap().get(modelKey);
methods = model.getDeclaredMethods();
Arrays.sort(methods, new MemberComparator<Method>());
TableLayout table = new TableLayout(this);
addModelFieldHeaders(table, inspector);
addModelDataRows(table, inspector);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
scrollView.setHorizontalScrollBarEnabled(true);
scrollView.setVerticalScrollBarEnabled(true);
scrollView.setFillViewport(true);
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
horizontalScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
horizontalScrollView.setHorizontalScrollBarEnabled(true);
horizontalScrollView.setVerticalScrollBarEnabled(true);
horizontalScrollView.setFillViewport(true);
scrollView.addView(horizontalScrollView);
horizontalScrollView.addView(table);
setContentView(scrollView);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mLinearLayout = new LinearLayout(getContext());
mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
mScrollView = new HorizontalScrollView(getContext());
mScrollView.addView(mLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mScrollView.setHorizontalScrollBarEnabled(isHorizontalScrollBarEnabled());
addView(mScrollView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
updateState();
}
private LinearLayout getPlatformList() {
LinearLayout llToolBar = new LinearLayout(getContext());
LinearLayout.LayoutParams lpTb = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llToolBar.setLayoutParams(lpTb);
TextView tvShareTo = new TextView(getContext());
int resId = getStringRes(activity, "share_to");
if (resId > 0) {
tvShareTo.setText(resId);
}
tvShareTo.setTextColor(0xffcfcfcf);
tvShareTo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
int dp_9 = dipToPx(getContext(), 9);
LinearLayout.LayoutParams lpShareTo = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpShareTo.gravity = Gravity.CENTER_VERTICAL;
lpShareTo.setMargins(dp_9, 0, 0, 0);
tvShareTo.setLayoutParams(lpShareTo);
llToolBar.addView(tvShareTo);
HorizontalScrollView sv = new HorizontalScrollView(getContext());
sv.setHorizontalScrollBarEnabled(false);
sv.setHorizontalFadingEdgeEnabled(false);
LinearLayout.LayoutParams lpSv = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSv.setMargins(dp_9, dp_9, dp_9, dp_9);
sv.setLayoutParams(lpSv);
llToolBar.addView(sv);
llPlat = new LinearLayout(getContext());
llPlat.setLayoutParams(new HorizontalScrollView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
sv.addView(llPlat);
return llToolBar;
}
@Override
public View generateView(
@NonNull final AppCompatActivity activity,
@Nullable final Integer textColor,
@Nullable final Float textSize,
final boolean showLinkButtons) {
final TableLayout table = new TableLayout(activity);
for(final BodyElement element : mElements) {
final View view = element.generateView(activity, textColor, textSize, showLinkButtons);
table.addView(view);
}
table.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
table.setDividerDrawable(new ColorDrawable(Color.GRAY));
table.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
final HorizontalScrollView scrollView = new HorizontalScrollView(activity);
scrollView.addView(table);
return scrollView;
}
private View createSucessView() {
view = View.inflate(getApplicationContext(), R.layout.activity_detail, null);
FrameLayout fl_appinfo = (FrameLayout) view.findViewById(R.id.fl_appinfo);
HorizontalScrollView hl_screen_shots = (HorizontalScrollView) view.findViewById(R.id.hl_screen_shots);
FrameLayout fl_changelog = (FrameLayout) view.findViewById(R.id.fl_changelog);
FrameLayout fl_comments = (FrameLayout) view.findViewById(R.id.fl_comments);
FrameLayout fl_introducation = (FrameLayout) view.findViewById(R.id.fl_introducation);
FrameLayout fl_versioninfos = (FrameLayout) view.findViewById(R.id.fl_versioninfos);
FrameLayout fl_bottom = (FrameLayout) view.findViewById(R.id.fl_bottom);
//appInfo部分
AppInfosHolder infosHolder = new AppInfosHolder();
infosHolder.refreshView(load, load.getHost(), 0);
fl_appinfo.addView(infosHolder.getConvertView());
//截图部分
AppScreensHolder screensHolder = new AppScreensHolder();
screensHolder.refreshView(load, load.getHost(), 0);
hl_screen_shots.addView(screensHolder.getConvertView());
//介绍部分
AppIntroHolder introHolder = new AppIntroHolder();
introHolder.refreshView(load, load.getHost(), 0);
fl_introducation.addView(introHolder.getConvertView());
//日志部分
AppChangeLogHolder logHolder = new AppChangeLogHolder();
logHolder.refreshView(load, load.getHost(), 0);
fl_changelog.addView(logHolder.getConvertView());
//评论部分
AppCommentsHolder commentsHolder = new AppCommentsHolder();
commentsHolder.refreshView(load, load.getHost(), 0);
fl_comments.addView(commentsHolder.getConvertView());
//版本信息部分
AppVersionsHolder versionsHolder = new AppVersionsHolder();
versionsHolder.refreshView(load, load.getHost(), 0);
fl_versioninfos.addView(versionsHolder.getConvertView());
return view;
}
public MaterialTabHost(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
scrollView = new HorizontalScrollView(context);
scrollView.setOverScrollMode(HorizontalScrollView.OVER_SCROLL_NEVER);
scrollView.setHorizontalScrollBarEnabled(false);
layout = new LinearLayout(context);
scrollView.addView(layout);
// get attributes
if(attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.MaterialTabHost, 0, 0);
try {
// custom attributes
hasIcons = a.getBoolean(R.styleable.MaterialTabHost_hasIcons, false);
primaryColor = a.getColor(R.styleable.MaterialTabHost_materialTabsPrimaryColor, Color.parseColor("#009688"));
accentColor = a.getColor(R.styleable.MaterialTabHost_accentColor,Color.parseColor("#00b0ff"));
iconColor = a.getColor(R.styleable.MaterialTabHost_iconColor,Color.WHITE);
textColor = a.getColor(R.styleable.MaterialTabHost_textColor,Color.WHITE);
} finally {
a.recycle();
}
}
else {
hasIcons = false;
}
this.isInEditMode();
scrollable = false;
isTablet = this.getResources().getBoolean(R.bool.isTablet);
density = this.getResources().getDisplayMetrics().density;
tabSelected = 0;
// initialize tabs list
tabs = new LinkedList<MaterialTab>();
// set background color
super.setBackgroundColor(primaryColor);
}