下面列出了android.widget.Button#setLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void initView(int length) {
LayoutParams layoutParams = new LayoutParams(-1, -1);
LinearLayout layout = new LinearLayout(this.mContext);
layout.setLayoutParams(layoutParams);
layout.setOrientation(0);
this.buttons = new Button[length];
this.length = length;
for (int i = 0; i < length; i++) {
LayoutParams btnLayoutParams = new LayoutParams(DensityUtil.dip2px(this.mContext, 13.0f), DensityUtil.dip2px(this.mContext, 3.0f));
Button button = new Button(this.mContext);
button.setBackgroundResource(R.drawable.host_main_btn_selected);
btnLayoutParams.leftMargin = DensityUtil.dip2px(this.mContext, 6.0f);
button.setLayoutParams(btnLayoutParams);
this.buttons[i] = button;
layout.addView(button);
}
addView(layout);
}
@Override
protected void onInitData() {
mTitle.setText("Http test");
String[] bttxt = getResources().getStringArray(R.array.http_list);
if (bttxt != null) {
for (int i = 0; i < bttxt.length; i++) {
Button bt = new Button(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
lp.setMargins(margin, margin, margin, margin);
bt.setId(i);
bt.setText(bttxt[i]);
bt.setOnClickListener(this);
bt.setLayoutParams(lp);
mContainer.addView(bt);
}
}
}
/**
* set positive button
*
* @param text the name of button
*/
public void setPositiveButton(String text, final View.OnClickListener listener)
{
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setTextColor(Color.argb(255, 35, 159, 242));
button.setText(text);
button.setGravity(Gravity.CENTER);
button.setTextSize(14);
button.setPadding(dip2px(12), 0, dip2px(32), dip2px(BUTTON_BOTTOM));
button.setOnClickListener(listener);
mButtonLayout.addView(button);
}
/**
* set negative button
*
* @param text the name of button
*/
public void setNegativeButton(String text, final View.OnClickListener listener)
{
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setText(text);
button.setTextColor(Color.argb(222, 0, 0, 0));
button.setTextSize(14);
button.setGravity(Gravity.CENTER);
button.setPadding(0, 0, 0, dip2px(8));
button.setOnClickListener(listener);
if (mButtonLayout.getChildCount() > 0)
{
params.setMargins(20, 0, 10, dip2px(BUTTON_BOTTOM));
button.setLayoutParams(params);
mButtonLayout.addView(button, 1);
}
else
{
button.setLayoutParams(params);
mButtonLayout.addView(button);
}
}
void hideOneButton(Button mainButton, Button secondaryButton, Button tertiaryButton){
tertiaryButton.setVisibility(View.GONE);
LinearLayout.LayoutParams mainLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,3);
mainButton.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams secondaryLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,3);
secondaryButton.setLayoutParams(secondaryLayoutParams);
LinearLayout.LayoutParams tertiaryLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,0);
tertiaryButton.setLayoutParams(tertiaryLayoutParams);
}
@Override
public void createViews(Context context, ViewGroup layout) {
Button button = new Button(context);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
button.setText("Click Me!");
PixateFreestyle.setStyleClass(button, "myButton");
layout.addView(button);
addView(button);
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.tableForButtons);
for (int row = 0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COLS; col++){
final int FINAL_COL = col;
final int FINAL_ROW = row;
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
1.0f));
button.setText("" + col + "," + row);
// Make text not clip on small buttons
button.setPadding(0, 0, 0, 0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gridButtonClicked(FINAL_COL, FINAL_ROW);
}
});
tableRow.addView(button);
buttons[row][col] = button;
}
}
}
void hideSecondaryButton(Button mainButton, Button secondaryButton){
secondaryButton.setVisibility(View.GONE);
LinearLayout.LayoutParams mainLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,2);
mainButton.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams secondaryLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,0);
secondaryButton.setLayoutParams(secondaryLayoutParams);
}
private void addButtonView() {
i++;
Button button = new Button(getActivity());
button.setText("button" + i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
layoutTransitionGroup.addView(button, 0);
}
private void addDemo(String demoName, Class<? extends Activity> activityClass) {
Button b = new Button(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
b.setLayoutParams(layoutParams);
b.setText(demoName);
b.setTag(activityClass);
b.setOnClickListener(this);
mListView.addView(b);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
drawer = (MaterialNavigationDrawer) getActivity();
Button button = new Button(this.getActivity());
button.setText("start child fragment");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.changeFragment(new ChildFragment(), "Child Title");
// normally currentSection gets unselect on setCustomFragment call
// in the next relase, i will add a new method without unselect
drawer.getCurrentSectionFragment().select();
// call on current git head. drawer.getCurrentSectionFragment().select(); is not needed
// drawer.setCustomFragment(drawer.getCurrentSectionFragment().getTargetFragment(), drawer.getCurrentSectionFragment().getFragmentTitle(), true, false);
}
});
return button;
}
private Button createMaterialMotionButton(String label,
final float cx1, final float cy1, final float cx2, final float cy2,
float weight, final ControlPointCallback callback) {
Button button = new Button(this);
LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = weight;
button.setLayoutParams(params);
button.setText("F out L in");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Animate the control points to their new values
final float oldCx1 = mVisualizer.getCx1();
final float oldCy1 = mVisualizer.getCy1();
final float oldCx2 = mVisualizer.getCx2();
final float oldCy2 = mVisualizer.getCy2();
ValueAnimator anim = ValueAnimator.ofFloat(0, 1).setDuration(100);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float t = valueAnimator.getAnimatedFraction();
mVisualizer.setCubicInterpolator(oldCx1 + t * (cx1 - oldCx1),
oldCy1 + t * (cy1 - oldCy1),
oldCx2 + t * (cx2 - oldCx2),
oldCy2 + t * (cy2 - oldCy2), callback);
}
});
anim.start();
}
});
return button;
}
private void initPageView() {
flPage = new FrameLayout(getContext());
flPage.setOnClickListener(this);
// container of the platform gridview
LinearLayout llPage = new LinearLayout(getContext()) {
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
llPage.setOrientation(LinearLayout.VERTICAL);
int resId = getBitmapRes(getContext(), "share_vp_back");
if (resId > 0) {
llPage.setBackgroundResource(resId);
}
FrameLayout.LayoutParams lpLl = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lpLl.gravity = Gravity.BOTTOM;
llPage.setLayoutParams(lpLl);
flPage.addView(llPage);
// gridview
grid = new PlatformGridView(getContext());
LinearLayout.LayoutParams lpWg = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
grid.setLayoutParams(lpWg);
llPage.addView(grid);
// cancel button
btnCancel = new Button(getContext());
btnCancel.setTextColor(0xffffffff);
btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
resId = getStringRes(getContext(), "cancel");
if (resId > 0) {
btnCancel.setText(resId);
}
btnCancel.setPadding(0, 0, 0, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 5));
resId = getBitmapRes(getContext(), "btn_cancel_back");
if (resId > 0) {
btnCancel.setBackgroundResource(resId);
}
LinearLayout.LayoutParams lpBtn = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 45));
int dp_10 = cn.sharesdk.framework.utils.R.dipToPx(getContext(), 10);
lpBtn.setMargins(dp_10, dp_10, dp_10, dp_10);
btnCancel.setLayoutParams(lpBtn);
llPage.addView(btnCancel);
}
private void centerButton(Button button) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
params.gravity = Gravity.CENTER_HORIZONTAL;
params.weight = 0.5f;
button.setLayoutParams(params);
}
private void initPageView() {
flPage = new FrameLayout(getContext());
flPage.setOnClickListener(this);
// 宫格列表的容器,为了“下对齐”,在外部包含了一个FrameLayout
LinearLayout llPage = new LinearLayout(getContext()) {
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
llPage.setOrientation(LinearLayout.VERTICAL);
int resId = getBitmapRes(getContext(), "share_vp_back");
if (resId > 0) {
llPage.setBackgroundResource(resId);
}
FrameLayout.LayoutParams lpLl = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lpLl.gravity = Gravity.BOTTOM;
llPage.setLayoutParams(lpLl);
flPage.addView(llPage);
// 宫格列表
grid = new PlatformGridView(getContext());
grid.setEditPageBackground(bgView);
LinearLayout.LayoutParams lpWg = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
grid.setLayoutParams(lpWg);
llPage.addView(grid);
// 取消按钮
btnCancel = new Button(getContext());
btnCancel.setTextColor(0xffffffff);
btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
resId = getStringRes(getContext(), "cancel");
if (resId > 0) {
btnCancel.setText(resId);
}
btnCancel.setPadding(0, 0, 0, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 5));
resId = getBitmapRes(getContext(), "btn_cancel_back");
if (resId > 0) {
btnCancel.setBackgroundResource(resId);
}
LinearLayout.LayoutParams lpBtn = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 45));
int dp_10 = cn.sharesdk.framework.utils.R.dipToPx(getContext(), 10);
lpBtn.setMargins(dp_10, dp_10, dp_10, dp_10);
btnCancel.setLayoutParams(lpBtn);
llPage.addView(btnCancel);
}
protected void setupButton(Button button, LinearLayout.LayoutParams params) {
button.setSingleLine(false);
button.setAllCaps(true);
button.setLayoutParams(params);
}
private void initPageView() {
flPage = new FrameLayout(getContext());
flPage.setOnClickListener(this);
// 宫格列表的容器,为了“下对齐”,在外部包含了一个FrameLayout
LinearLayout llPage = new LinearLayout(getContext()) {
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
llPage.setOrientation(LinearLayout.VERTICAL);
int resId = getBitmapRes(getContext(), "share_vp_back");
if (resId > 0) {
llPage.setBackgroundResource(resId);
}
FrameLayout.LayoutParams lpLl = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lpLl.gravity = Gravity.BOTTOM;
llPage.setLayoutParams(lpLl);
flPage.addView(llPage);
// 宫格列表
grid = new PlatformGridView(getContext());
grid.setEditPageBackground(bgView);
LinearLayout.LayoutParams lpWg = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
grid.setLayoutParams(lpWg);
llPage.addView(grid);
// 取消按钮
btnCancel = new Button(getContext());
btnCancel.setTextColor(0xffffffff);
btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
resId = getStringRes(getContext(), "cancel");
if (resId > 0) {
btnCancel.setText(resId);
}
btnCancel.setPadding(0, 0, 0, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 5));
resId = getBitmapRes(getContext(), "btn_cancel_back");
if (resId > 0) {
btnCancel.setBackgroundResource(resId);
}
LinearLayout.LayoutParams lpBtn = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 45));
int dp_10 = cn.sharesdk.framework.utils.R.dipToPx(getContext(), 10);
lpBtn.setMargins(dp_10, dp_10, dp_10, dp_10);
btnCancel.setLayoutParams(lpBtn);
llPage.addView(btnCancel);
}
private AlertDialog.Builder createDirectoryChooserDialog(String title, List<String> listItems,
DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(m_context);
// Create custom view for AlertDialog title containing
// current directory TextView and possible 'New folder' button.
// Current directory TextView allows long directory path to be wrapped to multiple lines.
LinearLayout titleLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.VERTICAL);
m_titleView = new TextView(m_context);
m_titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
m_titleView.setTextAppearance(m_context, android.R.style.TextAppearance_Large);
m_titleView.setTextColor(m_context.getResources().getColor(android.R.color.white));
m_titleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
m_titleView.setText(title);
Button newDirButton = new Button(m_context);
newDirButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
newDirButton.setText(m_context.getString(R.string.new_folder));
newDirButton.setOnClickListener(v -> {
final EditText input = new EditText(m_context);
// Show new folder name input dialog
new AlertDialog.Builder(m_context).
setTitle("New folder name").
setView(input).setPositiveButton(m_context.getString(R.string.global_ok), (dialog, whichButton) -> {
Editable newDir = input.getText();
String newDirName = newDir.toString();
// Create new directory
if (createSubDir(m_dir + "/" + newDirName)) {
// Navigate into the new directory
m_dir += "/" + newDirName;
updateDirectory();
} else {
Toast.makeText(
m_context, "Failed to create '" + newDirName +
"' folder", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton(m_context.getString(R.string.global_cancel), null).show();
});
if (!m_isNewFolderEnabled) {
newDirButton.setVisibility(View.GONE);
}
titleLayout.addView(m_titleView);
titleLayout.addView(newDirButton);
dialogBuilder.setCustomTitle(titleLayout);
m_listAdapter = createListAdapter(listItems);
dialogBuilder.setSingleChoiceItems(m_listAdapter, -1, onClickListener);
dialogBuilder.setCancelable(false);
return dialogBuilder;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = new RecyclerView(this);
LayoutUtil.setContentView(this, recyclerView);
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
layoutManager1 = new GridLayoutManager(this, 2);
layoutManager2 = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
data.addAll(DataManager.loadData(getBaseContext()));
final CommonRcvAdapter<DemoModel> adapter = initAdapter();
wrapper = new RcvAdapterWrapper(adapter, recyclerView.getLayoutManager());
final Button header = new Button(this);
header.setText("Header\n\n (click to add)");
header.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, 300));
final Button footer = new Button(this);
footer.setText("footer");
wrapper.setHeaderView(header);
wrapper.setFooterView(null);
final Button empty = new Button(this);
empty.setBackgroundColor(Color.RED);
empty.setText("empty text");
wrapper.setEmptyView(empty, recyclerView);
recyclerView.setAdapter(wrapper);
handItemClick();
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
data.reset(DataManager.loadData(getBaseContext(),10));
wrapper.setFooterView(footer);
}
}, 1000);
}
private void initPageView() {
flPage = new FrameLayout(getContext());
flPage.setOnClickListener(this);
// 宫格列表的容器,为了“下对齐”,在外部包含了一个FrameLayout
LinearLayout llPage = new LinearLayout(getContext()) {
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
llPage.setOrientation(LinearLayout.VERTICAL);
int resId = getBitmapRes(getContext(), "share_vp_back");
if (resId > 0) {
llPage.setBackgroundResource(resId);
}
FrameLayout.LayoutParams lpLl = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lpLl.gravity = Gravity.BOTTOM;
llPage.setLayoutParams(lpLl);
flPage.addView(llPage);
// 宫格列表
grid = new PlatformGridView(getContext());
grid.setEditPageBackground(bgView);
LinearLayout.LayoutParams lpWg = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
grid.setLayoutParams(lpWg);
llPage.addView(grid);
// 取消按钮
btnCancel = new Button(getContext());
btnCancel.setTextColor(0xffffffff);
btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
resId = getStringRes(getContext(), "cancel");
if (resId > 0) {
btnCancel.setText(resId);
}
btnCancel.setPadding(0, 0, 0, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 5));
resId = getBitmapRes(getContext(), "btn_cancel_back");
if (resId > 0) {
btnCancel.setBackgroundResource(resId);
}
LinearLayout.LayoutParams lpBtn = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 45));
int dp_10 = cn.sharesdk.framework.utils.R.dipToPx(getContext(), 10);
lpBtn.setMargins(dp_10, dp_10, dp_10, dp_10);
btnCancel.setLayoutParams(lpBtn);
llPage.addView(btnCancel);
}