下面列出了android.widget.CheckBox#setPadding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListItem listItem = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
View view = LayoutInflater.from(parent.getContext()).inflate(layoutResId, parent, false);
if (listItem.checkable) {
LinearLayout linearLayout = new LinearLayout(parent.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
linearLayout.addView(view, new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
CheckBox checkBox = new CheckBox(parent.getContext());
checkBox.setClickable(false);
checkBox.setFocusable(false);
int paddingRight = view.getPaddingRight() - (int) (4f * ResourceUtils.obtainDensity(view));
checkBox.setPadding(checkBox.getPaddingLeft(), checkBox.getPaddingTop(),
Math.max(checkBox.getPaddingRight(), paddingRight), checkBox.getPaddingBottom());
linearLayout.addView(checkBox, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewHolder.checkBox = checkBox;
view = linearLayout;
}
viewHolder.textView = view.findViewById(android.R.id.text1);
view.setTag(viewHolder);
convertView = view;
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(listItem.title);
if (listItem.checkable) {
viewHolder.checkBox.setChecked(listItem.checked);
}
return convertView;
}
/**
* @param context the context to use.
* @param attrs attributes.
* @param parentView parent
* @param label label
* @param value value
* @param constraintDescription constraints
* @param readonly if <code>false</code>, the item is disabled for editing.
*/
public GBooleanView(Context context, AttributeSet attrs, LinearLayout parentView, String label, String value,
String constraintDescription, boolean readonly) {
super(context, attrs);
LinearLayout textLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
textLayout.setLayoutParams(layoutParams);
textLayout.setOrientation(LinearLayout.VERTICAL);
parentView.addView(textLayout);
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(2, 2, 2, 2);
textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription);
textView.setTextColor(Compat.getColor(context, R.color.formcolor));
textLayout.addView(textView);
checkbox = new CheckBox(context);
checkbox.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
checkbox.setPadding(15, 5, 15, 5);
if (value != null) {
if (value.trim().toLowerCase().equals("true")) { //$NON-NLS-1$
checkbox.setChecked(true);
} else {
checkbox.setChecked(false);
}
}
checkbox.setEnabled(!readonly);
textLayout.addView(checkbox);
}
/**
* Set the syndication targets.
*/
public void setSyndicationTargets() {
LinearLayout syndicationLayout = findViewById(R.id.syndicationTargets);
String syndicationTargetsString = user.getSyndicationTargets();
if (syndicationLayout != null && syndicationTargetsString.length() > 0) {
syndicationLayout.removeAllViews();
JSONObject object;
try {
JSONArray itemList = new JSONArray(syndicationTargetsString);
if (itemList.length() > 0) {
TextView syn = new TextView(this);
syn.setText(R.string.syndicate_to);
syn.setPadding(20, 10, 0, 0);
syn.setTextSize(15);
syn.setTextColor(getResources().getColor(R.color.textColor));
syndicationLayout.addView(syn);
syndicationLayout.setPadding(10, 0,0, 0 );
}
for (int i = 0; i < itemList.length(); i++) {
object = itemList.getJSONObject(i);
Syndication syndication = new Syndication();
syndication.setUid(object.getString("uid"));
syndication.setName(object.getString("name"));
if (object.has("checked")) {
syndication.setChecked(object.getBoolean("checked"));
}
syndicationTargets.add(syndication);
CheckBox ch = new CheckBox(this);
ch.setText(syndication.getName());
ch.setId(i);
ch.setTextSize(15);
ch.setPadding(0, 10, 0, 10);
ch.setTextColor(getResources().getColor(R.color.textColor));
if (syndication.isChecked()) {
ch.setChecked(true);
}
syndicationLayout.addView(ch);
}
}
catch (JSONException e) {
String message = String.format(getString(R.string.syndication_targets_parse_error), e.getMessage());
final Snackbar snack = Snackbar.make(layout, message, Snackbar.LENGTH_INDEFINITE);
snack.setAction(getString(R.string.close), new View.OnClickListener() {
@Override
public void onClick(View v) {
snack.dismiss();
}
}
);
snack.show();
}
}
}
/**
* Creates a textView with the given string, and adds it to the item list
* @param text String to add to the list
*/
private void addUrlView(String text){
final String rowName = text;
// Create a new Layout to hold items
final LinearLayout itemRow = new LinearLayout(getContext());
itemRow.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.HORIZONTAL));
itemRow.setGravity(Gravity.CENTER);
// itemRow.setPadding(0,48,0, 48);
// itemRow.setBackground(getResources().getDrawable(R.drawable.delete_bg));
ImageButton deleteButton = new ImageButton(getContext());
deleteButton.setImageResource(R.drawable.delete_forever);
deleteButton.setBackground(null);
deleteButton.setVisibility(View.GONE);
deleteButton.setPadding(48,48,48, 48);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(removeStringFromSet(text)){
labelHolder = (LinearLayout)ViewAnimation.fadeOutAndRemove(itemRow, labelHolder, 250);
}
}
});
// Create checkbox
CheckBox check = new CheckBox(getContext());
check.setPadding(16,0,64,0);
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// setDeleteSelected();
}
});
// Create text
TextView nameText = new TextView(getContext());
nameText.setText(text);
LinearLayout.LayoutParams textLayout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
textLayout.setMargins(16, 16, 16, 16);
textLayout.gravity = Gravity.CENTER;
nameText.setLayoutParams(textLayout);
nameText.setPadding(32,32,32, 32);
nameText.setTextAppearance(getContext(), R.style.textAppearanceSubtitle1);
// Add everything
itemRow.addView(deleteButton);
// itemRow.addView(check);
itemRow.addView(nameText);
ViewAnimation.setSlideInFromRightAnimation(itemRow, 250);
labelHolder.addView(itemRow);
}
@SuppressWarnings("unchecked")
public SelectMultiWidget(Context context, FormEntryPrompt prompt) {
super(context, prompt);
mCheckboxes = new Vector<>();
mItems = getSelectChoices();
setOrientation(LinearLayout.VERTICAL);
Vector<Selection> ve = new Vector<>();
if (mPrompt.getAnswerValue() != null) {
ve = (Vector<Selection>)getCurrentAnswer().getValue();
}
//Is this safe enough from collisions?
buttonIdBase = Math.abs(mPrompt.getIndex().hashCode());
if (mItems != null) {
for (int i = 0; i < mItems.size(); i++) {
// no checkbox group so id by answer + offset
final CheckBox c = new CheckBox(getContext());
c.setId(buttonIdBase + i);
String markdownText = prompt.getSelectItemMarkdownText(mItems.get(i));
if (markdownText != null) {
c.setText(forceMarkdown(markdownText));
} else {
c.setText(prompt.getSelectChoiceText(mItems.get(i)));
}
c.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontSize);
c.setFocusable(!mPrompt.isReadOnly());
c.setEnabled(!mPrompt.isReadOnly());
int padding = (int)Math.floor(context.getResources().getDimension(R.dimen.select_padding));
c.setPadding(c.getPaddingLeft(), 0, padding, 0);
for (int vi = 0; vi < ve.size(); vi++) {
// match based on value, not key
if (mItems.get(i).getValue().equals(ve.elementAt(vi).getValue())) {
c.setChecked(true);
break;
}
}
//Note: This gets fired during setup as well, so this listener should only
//be added after everything about the checkbox is set up
// when clicked, check for readonly before toggling
c.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (!mCheckboxInit && mPrompt.isReadOnly()) {
if (buttonView.isChecked()) {
buttonView.setChecked(false);
} else {
buttonView.setChecked(true);
}
}
widgetEntryChanged();
});
mCheckboxes.add(c);
String audioURI =
mPrompt.getSpecialFormSelectChoiceText(mItems.get(i),
FormEntryCaption.TEXT_FORM_AUDIO);
String imageURI =
mPrompt.getSpecialFormSelectChoiceText(mItems.get(i),
FormEntryCaption.TEXT_FORM_IMAGE);
String videoURI = mPrompt.getSpecialFormSelectChoiceText(mItems.get(i), "video");
String bigImageURI = mPrompt.getSpecialFormSelectChoiceText(mItems.get(i), "big-image");
MediaLayout mediaLayout = MediaLayout.buildAudioImageVisualLayout(getContext(), c, audioURI, imageURI, videoURI, bigImageURI);
addView(mediaLayout);
mediaLayout.setPadding(0, padding, 0, padding);
mediaLayout.setOnClickListener(v -> c.performClick());
// Last, add the dividing line between elements (except for the last element)
ImageView divider = new ImageView(getContext());
divider.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
if (i != mItems.size() - 1) {
addView(divider);
}
}
}
mCheckboxInit = false;
}