android.widget.TextView.setGravity()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(212)

本文整理了Java中android.widget.TextView.setGravity()方法的一些代码示例,展示了TextView.setGravity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TextView.setGravity()方法的具体详情如下:
包路径:android.widget.TextView
类名称:TextView
方法名:setGravity

TextView.setGravity介绍

暂无

代码示例

代码示例来源:origin: scwang90/SmartRefreshLayout

protected TextView createMaskView(Context context, String text, int textSize, int gravity) {
  final TextView maskView = new TextView(context);
  maskView.setTextColor(Color.BLACK);
  maskView.setGravity(gravity | Gravity.CENTER_HORIZONTAL);
  maskView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
  maskView.setText(text);
  //noinspection UnnecessaryLocalVariable
  final View view = maskView;
  view.setBackgroundColor(Color.WHITE);
  return maskView;
}

代码示例来源:origin: square/dagger

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
 TextView tv = new TextView(getActivity());
 tv.setGravity(CENTER);
 tv.setText("Hello, World");
 return tv;
}

代码示例来源:origin: Ramotion/cardslider-android

@SuppressWarnings("deprecation")
@Override
public View makeView() {
  final TextView textView = new TextView(MainActivity.this);
  if (center) {
    textView.setGravity(Gravity.CENTER);
  }
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    textView.setTextAppearance(MainActivity.this, styleId);
  } else {
    textView.setTextAppearance(styleId);
  }
  return textView;
}

代码示例来源:origin: TommyLemon/APIJSON

@SuppressLint("InflateParams")
@Override
public View getRightMenu(Activity activity) {
  if (rightMenu == null) {
    rightMenu = (TextView) LayoutInflater.from(activity).inflate(R.layout.top_right_tv, null);
    rightMenu.setGravity(Gravity.CENTER);
    rightMenu.setText("设置");
    rightMenu.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onDragBottom(true);
      }
    });
  }
  return rightMenu;
}

代码示例来源:origin: stackoverflow.com

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

   @Override
   public View getInfoWindow(Marker arg0) {
     return null;
   }

   @Override
   public View getInfoContents(Marker marker) {

    LinearLayout info = new LinearLayout(mContext);
    info.setOrientation(LinearLayout.VERTICAL);

    TextView title = new TextView(mContext);
    title.setTextColor(Color.BLACK);
    title.setGravity(Gravity.CENTER);
    title.setTypeface(null, Typeface.BOLD);
    title.setText(marker.getTitle());

    TextView snippet = new TextView(mContext);
    snippet.setTextColor(Color.GRAY);
    snippet.setText(marker.getSnippet());

    info.addView(title);
    info.addView(snippet);

   return info;
  }
});

代码示例来源:origin: navasmdc/MaterialDesignLibrary

@Override
protected void onCreate(Bundle savedInstanceState) {
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  super.onCreate(savedInstanceState);
  setContentView(R.layout.number_indicator_spinner);
  setCanceledOnTouchOutside(false);
  RelativeLayout content = (RelativeLayout) this
      .findViewById(R.id.number_indicator_spinner_content);
  indicator = new Indicator(this.getContext());
  content.addView(indicator);
  numberIndicator = new TextView(getContext());
  numberIndicator.setTextColor(Color.WHITE);
  numberIndicator.setGravity(Gravity.CENTER);
  content.addView(numberIndicator);
  indicator.setLayoutParams(new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.FILL_PARENT,
      RelativeLayout.LayoutParams.FILL_PARENT));
}

代码示例来源:origin: ankidroid/Anki-Android

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.
 */
protected TextView createDefaultTabView(Context context) {
  TextView textView = new TextView(context);
  textView.setGravity(Gravity.CENTER);
  textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
  textView.setTypeface(Typeface.DEFAULT_BOLD);
  // use the Theme's selectableItemBackground to ensure that the View has a pressed state
  TypedValue outValue = new TypedValue();
  getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
      outValue, true);
  textView.setBackgroundResource(outValue.resourceId);
  
  // enable all-caps to match the Action Bar tab style
  textView.setAllCaps(true);
  int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
  textView.setPadding(padding, padding, padding, padding);
  return textView;
}

代码示例来源:origin: GitLqr/LQRWeChat

@Override
public void initView(View rootView) {
  mHeaderView = View.inflate(getActivity(), R.layout.header_rv_contacts, null);
  mTvNewFriendUnread = (TextView) mHeaderView.findViewById(R.id.tvNewFriendUnread);
  mFooterView = new TextView(getContext());
  ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dip2Px(50));
  mFooterView.setLayoutParams(params);
  mFooterView.setGravity(Gravity.CENTER);
  registerBR();
}

代码示例来源:origin: stackoverflow.com

myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

private void centerActionBarTitle() {
  int titleId = 0;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    titleId = getResources().getIdentifier("action_bar_title", "id", "android");
  } else {
    // This is the id is from your app's generated R class when 
    // ActionBarActivity is used for SupportActionBar
    titleId = R.id.action_bar_title;
  }

  // Final check for non-zero invalid id
  if (titleId > 0) {
    TextView titleTextView = (TextView) findViewById(titleId);
    DisplayMetrics metrics = getResources().getDisplayMetrics();

    // Fetch layout parameters of titleTextView 
    // (LinearLayout.LayoutParams : Info from HierarchyViewer)
    LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams();
    txvPars.gravity = Gravity.CENTER_HORIZONTAL;
    txvPars.width = metrics.widthPixels;
    titleTextView.setLayoutParams(txvPars);
    titleTextView.setGravity(Gravity.CENTER);
  }
}

代码示例来源:origin: hackware1993/MagicIndicator

@Override
public Object instantiateItem(ViewGroup container, int position) {
  TextView textView = new TextView(container.getContext());
  textView.setText(mDataList.get(position));
  textView.setGravity(Gravity.CENTER);
  textView.setTextColor(Color.BLACK);
  textView.setTextSize(24);
  container.addView(textView);
  return textView;
}

代码示例来源:origin: stackoverflow.com

AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
popupBuilder.setView(myMsg);

代码示例来源:origin: TommyLemon/APIJSON

@SuppressLint("InflateParams")
@Override
public View getLeftMenu(Activity activity) {
  if (leftMenu == null) {
    leftMenu = (TextView) LayoutInflater.from(activity).inflate(R.layout.top_right_tv, null);
    leftMenu.setGravity(Gravity.CENTER);
    leftMenu.setText("全部");//"筛选");
    leftMenu.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onDragBottom(false);
      }
    });
  }
  return leftMenu;
}

代码示例来源:origin: H07000223/FlycoDialog_Master

mTvTitle = new TextView(mContext);
mTvTitle.setGravity(Gravity.CENTER);
mTvTitle.setPadding(dp2px(10), dp2px(5), dp2px(10), dp2px(5));
mTvCancel = new TextView(mContext);
mTvCancel.setGravity(Gravity.CENTER);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.topMargin = dp2px(7);
lp.bottomMargin = dp2px(7);
mTvCancel.setLayoutParams(lp);

代码示例来源:origin: H07000223/FlycoDialog_Master

@Override
public View onCreateView() {
  /** title */
  mTvTitle.setGravity(Gravity.CENTER_VERTICAL);
  mTvTitle.setPadding(dp2px(20), dp2px(20), dp2px(20), dp2px(0));
  mTvTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
      LinearLayout.LayoutParams.WRAP_CONTENT));
  mLlContainer.addView(mTvTitle);
  /** content */
  mTvContent.setPadding(dp2px(20), dp2px(20), dp2px(20), dp2px(20));
  mTvContent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
      LinearLayout.LayoutParams.WRAP_CONTENT));
  mLlContainer.addView(mTvContent);
  /**btns*/
  mLlBtns.setGravity(Gravity.RIGHT);
  mLlBtns.addView(mTvBtnLeft);
  mLlBtns.addView(mTvBtnMiddle);
  mLlBtns.addView(mTvBtnRight);
  mTvBtnLeft.setPadding(dp2px(15), dp2px(8), dp2px(15), dp2px(8));
  mTvBtnRight.setPadding(dp2px(15), dp2px(8), dp2px(15), dp2px(8));
  mTvBtnMiddle.setPadding(dp2px(15), dp2px(8), dp2px(15), dp2px(8));
  mLlBtns.setPadding(dp2px(20), dp2px(0), dp2px(10), dp2px(10));
  mLlContainer.addView(mLlBtns);
  return mLlContainer;
}

代码示例来源:origin: stackoverflow.com

TextView txtTab = new TextView(this);

XmlResourceParser xrp = getResources().getXml(R.drawable.tab_text_selector);  
try {  
  ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);  
  txtTab.setTextColor(csl);  
} catch (Exception e) {  } 

txtTab.setBackgroundResource(R.drawable.tab_bg_selector);
txtTab.setGravity(Gravity.CENTER);
txtTab.setText("Agregar Idea");

代码示例来源:origin: Aspsine/SwipeToLoadLayout

@Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    TextView tv = new TextView(getActivity());
    tv.setText("Not developed yet. Support latter...");
    tv.setGravity(Gravity.CENTER);
    return tv;
  }
}

代码示例来源:origin: TommyLemon/APIJSON

@SuppressLint("InflateParams")
@Override
public View getLeftMenu(Activity activity) {
  if (leftMenu == null) {
    leftMenu = (TextView) LayoutInflater.from(activity).inflate(R.layout.top_right_tv, null);
    leftMenu.setGravity(Gravity.CENTER);
    leftMenu.setText("扫一扫");
    leftMenu.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onDragBottom(false);
      }
    });
  }
  return leftMenu;
}

代码示例来源:origin: stackoverflow.com

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // A normal textview
  TextView textView = new TextView(getApplicationContext());
  // set the text of textview with the string of shared cpp code
  textView.setText(concateneMyStringWithCppString("Javaaaa"));
  // normal things
  setContentView(textView);

  // only interface things nothng important
  textView.setTextSize(50);
  textView.setTextColor(Color.BLACK);
  textView.setGravity(Gravity.CENTER);
}

// very important
private native String concateneMyStringWithCppString(String myString);
static {
  System.loadLibrary("HelloCpp");
}

}

代码示例来源:origin: joyoyao/superCleanMaster

private void addTextTab(final int position, String title) {
  TextView tab = new TextView(getContext());
  tab.setText(title);
  tab.setGravity(Gravity.CENTER);
  tab.setSingleLine();
  addTab(position, tab);
}

代码示例来源:origin: stackoverflow.com

public class XYZ extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    Calendar c = Calendar.getInstance();
    System.out.println("Current time => "+c.getTime());

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = df.format(c.getTime());
    // formattedDate have current date/time
    Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();

   // Now we display formattedDate value in TextView
    TextView txtView = new TextView(this);
    txtView.setText("Current Date and Time : "+formattedDate);
    txtView.setGravity(Gravity.CENTER);
    txtView.setTextSize(20);
    setContentView(txtView);
  }

}

相关文章

TextView类方法