下面列出了android.widget.TextView#getParent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View inflate(LayoutInflater inflater) {
View view = inflater.inflate(R.layout.nim_contacts_item, null);
head = (HeadImageView) view.findViewById(R.id.contacts_item_head);
name = (TextView) view.findViewById(R.id.contacts_item_name);
time = (TextView) view.findViewById(R.id.contacts_item_time);
desc = (TextView) view.findViewById(R.id.contacts_item_desc);
// calculate
View parent = (View) desc.getParent();
if (parent.getMeasuredWidth() == 0) {
// xml中:50dp,包括头像的长度还有左右间距大小
parent.measure(View.MeasureSpec.makeMeasureSpec(ScreenUtil.getDisplayWidth() - ScreenUtil.dip2px(50.0f), View.MeasureSpec.EXACTLY), 0);
}
descTextViewWidth = (int) (parent.getMeasuredWidth() - desc.getPaint().measureText(PREFIX));
return view;
}
@SuppressLint("InflateParams")
protected View getView() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
mLicense = inflater.inflate(R.layout.license, null);
TextView mViewSource = (TextView) mLicense.findViewById(R.id.license_view_source);
mViewSource.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
LinearLayout mViewSourceCont = (LinearLayout) mViewSource.getParent();
mViewSourceCont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(context.getResources().getString(R.string.license_github_url)));
context.startActivity(i);
}
});
return mLicense;
}
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
// 因为TextView没有点击事件,所以点击TextView的非富文本时,super.onTouchEvent()返回false;
// 此时可以让TextView的父容器执行点击事件;
boolean isConsume = super.onTouchEvent(widget, buffer, event);
if (!isConsume && event.getAction() == MotionEvent.ACTION_UP) {
ViewParent parent = widget.getParent();
if (parent instanceof ViewGroup) {
// 获取被点击控件的父容器,让父容器执行点击;
((ViewGroup) parent).performClick();
}
}
return isConsume;
}
private void expectViewError(TextView view, String error) {
activity.onOptionsItemSelected(new RoboMenuItem(R.id.action_device_save));
verify(ipcamService, never()).add(any(IpcamModel.class));
verify(ipcamService, never()).update(any(IpcamModel.class));
assertEquals("should be required", error, view.getError());
// fix error:
// The specified child already has a parent. You must call removeView() on the child's parent first.
if (view.getParent() != null && view instanceof EditText) {
((ViewGroup) view.getParent()).removeView(view);
}
}
private void setError(TextView view, String text) {
TextInputLayout parent = (TextInputLayout) view.getParent();
// there is a small flashing when the error is set again
// the only way to fix that is to track if the error is
// currently shown, because for some reason TextInputLayout
// doesn't provide any getError methods.
parent.setError(text);
}
private void init() {
recordName = (TextView) findViewById(R.id.recordName);
parent = (View) recordName.getParent();
ODataRow row = mailMessage.browse(extra.getInt(OColumn.ROW_ID));
attachments.addAll(row.getM2MRecord("attachment_ids").browseEach());
if (attachments.size() > 0) {
loadAttachments = new LoadAttachments();
loadAttachments.execute();
}
horizontalScrollView = (LinearLayout) findViewById(R.id.attachmentsList);
baseModel = OModel.get(this, row.getString("model"), mailMessage.getUser().getAndroidName());
ODataRow record = baseModel.browse(baseModel.selectRowId(row.getInt("res_id")));
String name = record.getString(baseModel.getDefaultNameColumn());
recordName.setText(name);
recordName.setBackgroundColor(OStringColorUtil.getStringColor(this, name));
if (!row.getString("subject").equals("false"))
OControls.setText(parent, R.id.messageSubject, row.getString("subject"));
else
OControls.setGone(parent, R.id.messageSubject);
WebView messageBody = (WebView) findViewById(R.id.messageBody);
messageBody.setBackgroundColor(Color.TRANSPARENT);
messageBody.loadData(row.getString("body"), "text/html; charset=UTF-8", "UTF-8");
Bitmap author_image = BitmapUtils.getAlphabetImage(this, row.getString("author_name"));
String author_img = mailMessage.getAuthorImage(row.getInt(OColumn.ROW_ID));
if (!author_img.equals("false")) {
author_image = BitmapUtils.getBitmapImage(this, author_img);
}
OControls.setImage(parent, R.id.author_image, author_image);
OControls.setText(parent, R.id.authorName, row.getString("author_name"));
String date = ODateUtils.convertToDefault(row.getString("date"),
ODateUtils.DEFAULT_FORMAT, "MMM dd, yyyy hh:mm a");
OControls.setText(parent, R.id.messageDate, date);
}
private void init() {
recordName = (TextView) findViewById(R.id.recordName);
parent = (View) recordName.getParent();
ODataRow row = mailMessage.browse(extra.getInt(OColumn.ROW_ID));
attachments.addAll(row.getM2MRecord("attachment_ids").browseEach());
if (attachments.size() > 0) {
loadAttachments = new LoadAttachments();
loadAttachments.execute();
}
horizontalScrollView = (LinearLayout) findViewById(R.id.attachmentsList);
baseModel = OModel.get(this, row.getString("model"), mailMessage.getUser().getAndroidName());
ODataRow record = baseModel.browse(baseModel.selectRowId(row.getInt("res_id")));
String name = record.getString(baseModel.getDefaultNameColumn());
recordName.setText(name);
recordName.setBackgroundColor(OStringColorUtil.getStringColor(this, name));
if (!row.getString("subject").equals("false"))
OControls.setText(parent, R.id.messageSubject, row.getString("subject"));
else
OControls.setGone(parent, R.id.messageSubject);
WebView messageBody = (WebView) findViewById(R.id.messageBody);
messageBody.setBackgroundColor(Color.TRANSPARENT);
messageBody.loadData(row.getString("body"), "text/html; charset=UTF-8", "UTF-8");
Bitmap author_image = BitmapUtils.getAlphabetImage(this, row.getString("author_name"));
String author_img = mailMessage.getAuthorImage(row.getInt(OColumn.ROW_ID));
if (!author_img.equals("false")) {
author_image = BitmapUtils.getBitmapImage(this, author_img);
}
OControls.setImage(parent, R.id.author_image, author_image);
OControls.setText(parent, R.id.authorName, row.getString("author_name"));
String date = ODateUtils.convertToDefault(row.getString("date"),
ODateUtils.DEFAULT_FORMAT, "MMM dd, yyyy hh:mm a");
OControls.setText(parent, R.id.messageDate, date);
}
/**
* An even dirtier way to see if the TextView is part of the ActionBar
*
* @param view TextView to check is Title
* @return true if it is.
*/
@SuppressLint("NewApi")
protected static boolean isActionBarTitle(TextView view) {
if (matchesResourceIdName(view, ACTION_BAR_TITLE)) return true;
if (parentIsToolbarV7(view)) {
final android.support.v7.widget.Toolbar parent = (android.support.v7.widget.Toolbar) view.getParent();
return TextUtils.equals(parent.getTitle(), view.getText());
}
return false;
}
/**
* An even dirtier way to see if the TextView is part of the ActionBar
*
* @param view TextView to check is Title
* @return true if it is.
*/
@SuppressLint("NewApi")
protected static boolean isActionBarSubTitle(TextView view) {
if (matchesResourceIdName(view, ACTION_BAR_SUBTITLE)) return true;
if (parentIsToolbarV7(view)) {
final android.support.v7.widget.Toolbar parent = (android.support.v7.widget.Toolbar) view.getParent();
return TextUtils.equals(parent.getSubtitle(), view.getText());
}
return false;
}
@Override
public void run() {
if ((mTimes != null) && !mTimes.isDeleted()) {
checkKerahat();
if (mTimes.isAutoLocation())
mTitle.setText(mTimes.getName());
int next = mTimes.getNextTime();
int indicator = next;
if (Preferences.VAKIT_INDICATOR_TYPE.get().equals("next"))
indicator = indicator + 1;
for (int i = 0; i < 6; i++) {
TextView time = mPrayerTimes[i];
ViewGroup parent = (ViewGroup) time.getParent();
if (i == indicator - 1) {
time.setBackgroundResource(R.color.accent);
parent.getChildAt(parent.indexOfChild(time) - 1).setBackgroundResource(R.color.accent);
} else {
time.setBackgroundResource(R.color.transparent);
parent.getChildAt(parent.indexOfChild(time) - 1).setBackgroundResource(R.color.transparent);
}
}
String left = LocaleUtils.formatPeriod(DateTime.now(), mTimes.getTime(LocalDate.now(), next).toDateTime(), true);
mCountdown.setText(left);
if (++mThreeSecondCounter % 3 == 0) {
LocalDate date = LocalDate.now();
LocalDateTime sabah = mTimes.getSabahTime(date);
LocalDateTime asr = mTimes.getAsrThaniTime(date);
if (!(sabah.toLocalTime().equals(LocalTime.MIDNIGHT))) {
if (mThreeSecondCounter % 6 == 0) {
mPrayerTitles[Vakit.FAJR.ordinal()].setText(Vakit.FAJR.getString(0));
mPrayerTimes[Vakit.FAJR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getTime(date, Vakit.FAJR.ordinal()).toLocalTime()));
} else {
mPrayerTitles[Vakit.FAJR.ordinal()].setText(Vakit.FAJR.getString(1));
mPrayerTimes[Vakit.FAJR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getSabahTime(date).toLocalTime()));
}
}
if (!(asr.toLocalTime().equals(LocalTime.MIDNIGHT))) {
if (mThreeSecondCounter % 6 == 0) {
mPrayerTitles[Vakit.ASR.ordinal()].setText(Vakit.ASR.getString(0));
mPrayerTimes[Vakit.ASR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getTime(date, Vakit.ASR.ordinal()).toLocalTime()));
} else {
mPrayerTitles[Vakit.ASR.ordinal()].setText(Vakit.ASR.getString(1));
mPrayerTimes[Vakit.ASR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getAsrThaniTime(date).toLocalTime()));
}
}
}
}
mHandler.postDelayed(this, 1000);
}
@Override
public void run() {
if ((mTimes != null) && !mTimes.isDeleted()) {
checkKerahat();
if (mTimes.isAutoLocation())
mTitle.setText(mTimes.getName());
int next = mTimes.getNextTime();
int indicator = next;
if (Preferences.VAKIT_INDICATOR_TYPE.get().equals("next"))
indicator = indicator + 1;
for (int i = 0; i < 6; i++) {
TextView time = mPrayerTimes[i];
ViewGroup parent = (ViewGroup) time.getParent();
if (i == indicator - 1) {
time.setBackgroundResource(R.color.accent);
parent.getChildAt(parent.indexOfChild(time) - 1).setBackgroundResource(R.color.accent);
} else {
time.setBackgroundResource(R.color.transparent);
parent.getChildAt(parent.indexOfChild(time) - 1).setBackgroundResource(R.color.transparent);
}
}
String left = LocaleUtils.formatPeriod(DateTime.now(), mTimes.getTime(LocalDate.now(), next).toDateTime(), true);
mCountdown.setText(left);
if (++mThreeSecondCounter % 3 == 0) {
LocalDate date = LocalDate.now();
LocalDateTime sabah = mTimes.getSabahTime(date);
LocalDateTime asr = mTimes.getAsrThaniTime(date);
if (!(sabah.toLocalTime().equals(LocalTime.MIDNIGHT))) {
if (mThreeSecondCounter % 6 == 0) {
mPrayerTitles[Vakit.FAJR.ordinal()].setText(Vakit.FAJR.getString(0));
mPrayerTimes[Vakit.FAJR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getTime(date, Vakit.FAJR.ordinal()).toLocalTime()));
} else {
mPrayerTitles[Vakit.FAJR.ordinal()].setText(Vakit.FAJR.getString(1));
mPrayerTimes[Vakit.FAJR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getSabahTime(date).toLocalTime()));
}
}
if (!(asr.toLocalTime().equals(LocalTime.MIDNIGHT))) {
if (mThreeSecondCounter % 6 == 0) {
mPrayerTitles[Vakit.ASR.ordinal()].setText(Vakit.ASR.getString(0));
mPrayerTimes[Vakit.ASR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getTime(date, Vakit.ASR.ordinal()).toLocalTime()));
} else {
mPrayerTitles[Vakit.ASR.ordinal()].setText(Vakit.ASR.getString(1));
mPrayerTimes[Vakit.ASR.ordinal()].setText(LocaleUtils.formatTimeForHTML(mTimes.getAsrThaniTime(date).toLocalTime()));
}
}
}
}
mHandler.postDelayed(this, 1000);
}
private void removeError(TextView view) {
TextInputLayout parent = (TextInputLayout) view.getParent();
parent.setError(null);
}
@SuppressLint("NewApi")
public static void mirrorDialog(Dialog dialog, Context context) {
// Hebrew only
if (!Localization.isHebrewLocale(context)) {
return;
}
try {
// Get message text view
TextView message = (TextView) dialog.findViewById(android.R.id.message);
// Defy gravity
if (message != null) {
message.setGravity(Gravity.RIGHT);
}
// Get the title of text view
TextView title = (TextView) dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
// Defy gravity
title.setGravity(Gravity.RIGHT);
// Get list view (may not exist)
ListView listView = (ListView) dialog.findViewById(context.getResources().getIdentifier("select_dialog_listview", "id", "android"));
// Check if list & set RTL mode
if (listView != null) {
listView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
// Get title's parent layout
LinearLayout parent = ((LinearLayout) title.getParent());
// Get layout params
LinearLayout.LayoutParams originalParams = (LinearLayout.LayoutParams) parent.getLayoutParams();
// Set width to WRAP_CONTENT
originalParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
// Defy gravity
originalParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
// Set layout params
parent.setLayoutParams(originalParams);
}
catch (Exception exc) {
// Log failure to logcat
Log.d(Logging.TAG, "RTL failed", exc);
}
}
private ClickableToast(Holder holder) {
this.holder = holder;
Context context = holder.context;
float density = ResourceUtils.obtainDensity(context);
int innerPadding = (int) (8f * density);
LayoutInflater inflater = LayoutInflater.from(context);
View toast1 = inflater.inflate(LAYOUT_ID, null);
View toast2 = inflater.inflate(LAYOUT_ID, null);
TextView message1 = toast1.findViewById(android.R.id.message);
TextView message2 = toast2.findViewById(android.R.id.message);
View backgroundSource = null;
Drawable backgroundDrawable = toast1.getBackground();
if (backgroundDrawable == null) {
backgroundDrawable = message1.getBackground();
if (backgroundDrawable == null) {
View messageParent = (View) message1.getParent();
if (messageParent != null) {
backgroundDrawable = messageParent.getBackground();
backgroundSource = messageParent;
}
} else {
backgroundSource = message1;
}
} else {
backgroundSource = toast1;
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 100; i++) builder.append('W'); // Make long text
message1.setText(builder); // Avoid minimum widths
int measureSize = (int) (context.getResources().getConfiguration().screenWidthDp * density + 0.5f);
toast1.measure(View.MeasureSpec.makeMeasureSpec(measureSize, View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
toast1.layout(0, 0, toast1.getMeasuredWidth(), toast1.getMeasuredHeight());
Rect backgroundSourceTotalPadding = getViewTotalPadding(toast1, backgroundSource);
Rect messageTotalPadding = getViewTotalPadding(toast1, message1);
messageTotalPadding.left -= backgroundSourceTotalPadding.left;
messageTotalPadding.top -= backgroundSourceTotalPadding.top;
messageTotalPadding.right -= backgroundSourceTotalPadding.right;
messageTotalPadding.bottom -= backgroundSourceTotalPadding.bottom;
int horizontalPadding = Math.max(messageTotalPadding.left, messageTotalPadding.right) +
Math.max(message1.getPaddingLeft(), message1.getPaddingRight());
int verticalPadding = Math.max(messageTotalPadding.top, messageTotalPadding.bottom) +
Math.max(message1.getPaddingTop(), message1.getPaddingBottom());
ViewUtils.removeFromParent(message1);
ViewUtils.removeFromParent(message2);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setDividerDrawable(new ToastDividerDrawable(0xccffffff, (int) (density + 0.5f)));
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
linearLayout.setDividerPadding((int) (4f * density));
linearLayout.setTag(this);
linearLayout.addView(message1, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(message2, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
((LinearLayout.LayoutParams) message1.getLayoutParams()).weight = 1f;
linearLayout.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
partialClickDrawable = new PartialClickDrawable(backgroundDrawable);
message1.setBackground(null);
message2.setBackground(null);
linearLayout.setBackground(partialClickDrawable);
linearLayout.setOnTouchListener(partialClickDrawable);
message1.setPadding(0, 0, 0, 0);
message2.setPadding(innerPadding, 0, 0, 0);
message1.setSingleLine(true);
message2.setSingleLine(true);
message1.setEllipsize(TextUtils.TruncateAt.END);
message2.setEllipsize(TextUtils.TruncateAt.END);
container = linearLayout;
message = message1;
button = message2;
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
/**
* Creates a new instance of {@link VerticalMarqueeTextView.MarqueeRunnable}.
* @param textView The {@link TextView} to apply marquee effect.
*/
public MarqueeRunnable(final TextView textView) {
this.parent = (ViewGroup)textView.getParent();
this.textView = textView;
}