下面列出了android.view.View#setLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
int cCount = getChildCount();
if (cCount == 0)
return;
for (int i = 0; i < cCount; i++)
{
View view = getChildAt(i);
LinearLayout.LayoutParams lp = (LayoutParams) view
.getLayoutParams();
lp.weight = 0;
lp.width = getScreenWidth() / mTabVisibleCount;
view.setLayoutParams(lp);
}
setItemClickEvent();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
int viewType = getItemViewType(position);
if (convertView == null)
{
int layoutId = createViewByType(viewType);
convertView = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
//设置每个item为正方形
convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mImageLayoutSize));
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
if (viewType == VIEW_TYPE_CAMERA)
{
setCameraData(holder);
} else
{
setImageData(position, holder, mDataList.get(position));
}
return convertView;
}
public static void setupStatusBarView(Activity activity, ViewGroup decorViewGroup, boolean on, int colorRes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = activity.getWindow();
setTranslucentStatus(win, on);
View mStatusBarTintView = new View(activity);
int mStatusBarHeight = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
mStatusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
}
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, mStatusBarHeight);
params.gravity = Gravity.TOP;
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundResource(colorRes);
mStatusBarTintView.setVisibility(View.VISIBLE);
decorViewGroup.addView(mStatusBarTintView);
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
int counts = getChildCount();
if(counts == 0)
return;
for (int i = 0;i < counts; i++){
View view = getChildAt(i);
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.weight = 0;
params.width = getMeasuredWidth() / mDefaultVisibleCounts;
view.setLayoutParams(params);
}
setItemClickListener();
mLineWidth = getMeasuredWidth() / mDefaultVisibleCounts;
}
@Override
public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) {
View actionView = item.getActionView();
if (actionView == null || item.hasCollapsibleActionView()) {
if (!(convertView instanceof ActionMenuItemView)) {
convertView = null;
}
actionView = super.getItemView(item, convertView, parent);
}
actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE);
final ActionMenuView menuParent = (ActionMenuView) parent;
final ViewGroup.LayoutParams lp = actionView.getLayoutParams();
if (!menuParent.checkLayoutParams(lp)) {
actionView.setLayoutParams(menuParent.generateLayoutParams(lp));
}
return actionView;
}
private void init(AttributeSet attrs) {
TypedArray typedArray = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.TextPointView, 0, 0);
singleText = typedArray.getString(R.styleable.TextPointView_text);
circleColorRes = typedArray.getInt(R.styleable.TextPointView_redPointViewBgColor, R.color.bright_red);
textSize = typedArray.getDimensionPixelSize(R.styleable.TextPointView_textSize, DEFAULT_TEXT_SIZE);
typedArray.recycle();
circleView = new View(context);
circleView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setCircleBackgroundColor(circleColorRes);
textView = new TextView(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
textView.setLayoutParams(params);
textView.setTypeface(TypefaceUtils.load(context.getAssets(), "fonts/jianshi_default.otf"));
textView.setTextSize(textSize);
textView.setTextColor(ContextCompat.getColor(context, R.color.white));
textView.setText(singleText);
addView(circleView);
addView(textView);
}
/**
* To start list from center.
*
* @param context
* {@link Context}.
*/
public void startListFromCenter(Context context) {
View dummyHeader = new View(context);
AbsListView.LayoutParams dummyHeaderParams = new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, (displayHeight/2 - 70));
dummyHeader.setBackgroundColor(Color.TRANSPARENT);
dummyHeader.setLayoutParams(dummyHeaderParams);
addHeaderView(dummyHeader);
}
/**
* 设置标题栏MarginTop值为导航栏的高度
* Sets title bar margin top.
*
* @param activity the activity
* @param view the view
*/
public static void setTitleBarMarginTop(Activity activity, View... view) {
if (activity == null) {
return;
}
for (View v : view) {
if (v == null) {
continue;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight(activity);
Integer fitsHeight = (Integer) v.getTag(R.id.mn_ib_fits_layout_overlap);
if (fitsHeight == null) {
fitsHeight = 0;
}
if (fitsHeight != statusBarHeight) {
v.setTag(R.id.mn_ib_fits_layout_overlap, statusBarHeight);
ViewGroup.LayoutParams lp = v.getLayoutParams();
if (lp == null) {
lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) lp;
layoutParams.setMargins(layoutParams.leftMargin,
layoutParams.topMargin + statusBarHeight - fitsHeight,
layoutParams.rightMargin,
layoutParams.bottomMargin);
v.setLayoutParams(layoutParams);
}
}
}
}
private void setMarginFromBottom(View view)
{
if (view == null)
return;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.bottomMargin += ScreenUtils.getNavigationBarHeight(this);
view.setLayoutParams(layoutParams);
}
public VideoViewHolder(View itemView) {
super(itemView);
//以宽高比16:9的比例设置播放器的尺寸
int width = DisplayManager.screenWidthPixel(mContext);
int height = (int) (width * 1.0f / 16 * 9 + 0.5f);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
params.height = height;
params.width = width;
itemView.setLayoutParams(params);
ButterKnife.bind(this, itemView);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// 在这里将view的高度设置为精确高度,即可屏蔽向上滑动不占全屏的手势。
//如果不设置高度的话 会默认向上滑动时dialog覆盖全屏
View view = inflater.inflate(R.layout.dialog_comment, container, false);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getScreenHeight(getActivity()) / 2));
return view;
}
/**
* Initializes the view using the data held by {@code tile}. This should be called immediately
* after inflation.
* @param tile The tile that holds the data to populate this view.
* @param titleLines The number of text lines to use for the tile title.
* @param condensed Whether to use a condensed layout.
*/
public void initialize(Tile tile, int titleLines, boolean condensed) {
mTitleView.setLines(titleLines);
mUrl = tile.getUrl();
// TODO(mvanouwerkerk): Move this code to xml - https://crbug.com/695817.
if (condensed) {
Resources res = getResources();
setPadding(0, 0, 0, 0);
LayoutParams tileParams = (LayoutParams) getLayoutParams();
tileParams.width = res.getDimensionPixelOffset(R.dimen.tile_view_width_condensed);
setLayoutParams(tileParams);
LayoutParams iconParams = (LayoutParams) mIconView.getLayoutParams();
iconParams.setMargins(0,
res.getDimensionPixelOffset(R.dimen.tile_view_icon_margin_top_condensed), 0, 0);
mIconView.setLayoutParams(iconParams);
View highlightView = findViewById(R.id.tile_view_highlight);
LayoutParams highlightParams = (LayoutParams) highlightView.getLayoutParams();
highlightParams.setMargins(0,
res.getDimensionPixelOffset(R.dimen.tile_view_icon_margin_top_condensed), 0, 0);
highlightView.setLayoutParams(highlightParams);
LayoutParams titleParams = (LayoutParams) mTitleView.getLayoutParams();
titleParams.setMargins(0,
res.getDimensionPixelOffset(R.dimen.tile_view_title_margin_top_condensed), 0,
0);
mTitleView.setLayoutParams(titleParams);
}
renderTile(tile);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
View obtainView(int position, boolean[] isScrap) {
isScrap[0] = false;
View scrapView;
scrapView = mRecycler.getTransientStateView(position);
if (scrapView == null) {
scrapView = mRecycler.getScrapView(position);
}
View child;
if (scrapView != null) {
child = mAdapter.getView(position, scrapView, this);
if (child != scrapView) {
mRecycler.addScrapView(scrapView, position);
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
} else {
isScrap[0] = true;
}
} else {
child = mAdapter.getView(position, null, this);
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
}
if (mAdapterHasStableIds) {
final ViewGroup.LayoutParams vlp = child.getLayoutParams();
LayoutParams lp;
if (vlp == null) {
lp = (LayoutParams) generateDefaultLayoutParams();
} else if (!checkLayoutParams(vlp)) {
lp = (LayoutParams) generateLayoutParams(vlp);
} else {
lp = (LayoutParams) vlp;
}
lp.itemId = mAdapter.getItemId(position);
child.setLayoutParams(lp);
}
return child;
}
private void initTcpHeaderView(View view, TcpHeader tcpHeader, int start) {
TextView txtvwTcpSourcePort = (TextView) view.findViewById(R.id.txtvwTcpSourcePort);
txtvwTcpSourcePort.setText("源端口号:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getSourcePort());
TextView txtvwTcpDestPort = (TextView) view.findViewById(R.id.txtvwTcpDestPort);
txtvwTcpDestPort.setText("目的端口号:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getDestPort());
TextView txtvwTcpSeqNumber = (TextView) view.findViewById(R.id.txtvwTcpSeqNumber);
txtvwTcpSeqNumber.setText("序号:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getSequenceNumber());
TextView txtvwTcpAckNumber = (TextView) view.findViewById(R.id.txtvwTcpAckNumber);
txtvwTcpAckNumber.setText("应答序号:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getAckNumber());
TextView txtvwTcpDataOffset = (TextView) view.findViewById(R.id.txtvwTcpDataOffset);
txtvwTcpDataOffset.setText("头长度:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getHeaderLength());
TextView txtvwTcpReserve = (TextView) view.findViewById(R.id.txtvwTcpReserve);
txtvwTcpReserve.setText("保留字段:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getReserve());
TextView txtvwTcpFlags = (TextView) view.findViewById(R.id.txtvwTcpFlags);
txtvwTcpFlags.setText("标志位:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getFlagsString());
TextView txtvwTcpWindowSize = (TextView) view.findViewById(R.id.txtvwTcpWindowSize);
txtvwTcpWindowSize.setText("窗口大小:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getWindowSize());
TextView txtvwTcpCheckSum = (TextView) view.findViewById(R.id.txtvwTcpCheckSum);
txtvwTcpCheckSum.setText("校验和:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getChecksum());
TextView txtvwTcpUrgent = (TextView) view.findViewById(R.id.txtvwTcpUrgent);
txtvwTcpUrgent.setText("紧急指针:"+TEXTVIEW_TEXT_ALIGN+tcpHeader.getUrgent());
TextView txtvwTcpOptions = (TextView) view.findViewById(R.id.txtvwTcpOptions);
String tcpOptionsString = new String();
tcpOptionsString += "选项:"+TEXTVIEW_TEXT_ALIGN;
byte[] options = tcpHeader.getOptions();
int count = 0;
if(null != options) {
for(byte data:options) {
tcpOptionsString += String.format("%02x ", data);
++count;
if(count%8 == 0){
tcpOptionsString += '\n';
}
}
}
txtvwTcpOptions.setText(tcpOptionsString);
/* 设置边距 */
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5, 5, 5, 5);
view.setLayoutParams(layoutParams);
}
/**
* 初始化整个布局
*
* @param contentView 内容区(可以是一个布局,也可以是一个控件)
* @param listData 存放多个下拉列表的数据,可指定某一列数据变化(联动时,只能修改不能重新new)
*/
public void init(View contentView, List<Map<String, String>> listData) {
if (listData == null) {
throw new IllegalArgumentException("下拉列表数据不能为空");
}
if (cols != listData.size()) {
throw new IllegalArgumentException("下拉列表的列数与数据个数不一致,列数为" + cols + ",数据个数为" + listData.size());
}
this.mDropdownListStringData = listData;
//初始化所有动画
dropdown_in = AnimationUtils.loadAnimation(mContext, R.anim.dropdown_in);
dropdown_out = AnimationUtils.loadAnimation(mContext, R.anim.dropdown_out);
dropdown_mask_out = AnimationUtils.loadAnimation(mContext, R.anim.dropdown_mask_out);
//根布局(LinearLayout)
mRoot = new LinearLayout(mContext);
mRoot.setOrientation(LinearLayout.VERTICAL);
mRoot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mRoot.setGravity(Gravity.CENTER);
//1、下拉按钮区(LinearLayout)
LinearLayout llTop = new LinearLayout(mContext);
llTop.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, topHeight));
llTop.setBackgroundColor(topBg);
llTop.setGravity(Gravity.CENTER);
//根据下拉按钮个数创建下拉按钮
for (i = 0; i < cols; i++) {
LQRDropdownButton dropdownButton = new LQRDropdownButton(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1;
dropdownButton.setLayoutParams(params);
llTop.addView(dropdownButton);
//设置按钮样式
setDropdownButtonStyle(dropdownButton);
mDropdownButtonList.add(dropdownButton);
if (i != cols - 1) {
View view = new View(mContext);
view.setLayoutParams(new LinearLayout.LayoutParams(topSplitLineWidth, topSplitLineHeight));
view.setBackgroundColor(topSplitLineColor);
llTop.addView(view);
}
}
//2、分割线(View)
mVLine = new View(mContext);
mVLine.setLayoutParams(new LinearLayout.LayoutParams(splitLineWidth, splitLineHeight));
mVLine.setBackgroundColor(splitLineColor);
//3、下拉列表区(FrameLayout)
mFlContent = new FrameLayout(mContext);
mFlContent.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//将内容区放置最底
if (contentView != null) {
mFlContent.addView(contentView);
}
//创建遮盖层
mask = new View(mContext);
mask.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mask.setBackgroundColor(maskBg);
//遮盖层点击事件
mask.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
maskClick(v);
}
});
mFlContent.addView(mask);
//根据下拉按钮个数创建对应个数的下拉列表
for (i = 0; i < cols; i++) {
LQRDropdownListView dropdownListView = new LQRDropdownListView(mContext);
dropdownListView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//设置下拉列表和item样式
setDropdownListAndItemViewStyle(dropdownListView);
mFlContent.addView(dropdownListView);
mDropdownListViewList.add(dropdownListView);
}
mRoot.addView(llTop);
mRoot.addView(mVLine);
mRoot.addView(mFlContent);
this.addView(mRoot);
dropdownButtonsController.init();
}
private void createBoardUI(@NonNull Board board) {
final ViewGroup canvasView = mBoardContentView;
canvasView.removeAllViews();
final int kLedSize = (int) MetricsUtils.convertDpToPixel(getContext(), kLedPixelSize);
final int canvasViewWidth = canvasView.getWidth();
final int canvasViewHeight = canvasView.getHeight();
final int boardWidth = board.width * kLedSize;
final int boardHeight = board.height * kLedSize;
final int marginLeft = (canvasViewWidth - boardWidth) / 2;
final int marginTop = (canvasViewHeight - boardHeight) / 2;
for (int j = 0, k = 0; j < board.height; j++) {
for (int i = 0; i < board.width; i++, k++) {
View ledView = LayoutInflater.from(getContext()).inflate(R.layout.layout_neopixel_led, canvasView, false);
Button ledButton = ledView.findViewById(R.id.ledButton);
ledButton.setOnClickListener(view -> {
if (mBoard != null) {
int tag = (Integer) view.getTag();
byte x = (byte) (tag % mBoard.width);
byte y = (byte) (tag / mBoard.width);
Log.d(TAG, "led (" + x + "," + y + ")");
setViewBackgroundColor(view, mCurrentColor);
setPixelColor(mCurrentColor, mColorW, x, y, null);
mBoardCachedColors.set(y * mBoard.width + x, mCurrentColor);
}
});
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(kLedSize, kLedSize);
layoutParams.leftMargin = i * kLedSize + marginLeft;
layoutParams.topMargin = j * kLedSize + marginTop;
ledView.setLayoutParams(layoutParams);
ledButton.setTag(k);
setViewBackgroundColor(ledButton, kDefaultLedColor);
canvasView.addView(ledView);
}
}
// Setup initial scroll and scale
resetScaleAndPanning(board);
}
private boolean addNewLayout(View targetView) {
// check current views
if (getParent() != null || targetView == null || targetView.getParent() == null || viewContainerId != -1) {
return false;
}
ViewGroup parentContainer = (ViewGroup) targetView.getParent();
// if the current view is framelayout
if (targetView.getParent() instanceof FrameLayout) {
((FrameLayout) targetView.getParent()).addView(this);
} else if (targetView.getParent() instanceof ViewGroup) {
// get the index of the tagretview
int targetViewIndex = parentContainer.indexOfChild(targetView);
// generate new ID
viewContainerId = generateViewId();
// if relative layout
if (targetView.getParent() instanceof RelativeLayout) {
// loop through the tagetView parent childs
for (int i = 0; i < parentContainer.getChildCount(); i++) {
if (i == targetViewIndex) {
continue;
}
View view = parentContainer.getChildAt(i);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
for (int j = 0; j < layoutParams.getRules().length; j++) {
if (layoutParams.getRules()[j] == targetView.getId()) {
layoutParams.getRules()[j] = viewContainerId;
}
}
view.setLayoutParams(layoutParams);
}
}
parentContainer.removeView(targetView);
// new layout
FrameLayout lightTextViewContainer = new FrameLayout(getContext());
ViewGroup.LayoutParams targetLayoutParam = targetView.getLayoutParams();
lightTextViewContainer.setLayoutParams(targetLayoutParam);
targetView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// add target and label in this layout
lightTextViewContainer.addView(targetView);
lightTextViewContainer.addView(this);
lightTextViewContainer.setId(viewContainerId);
// add layout in parent container
parentContainer.addView(lightTextViewContainer, targetViewIndex, targetLayoutParam);
}
return true;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_flexiblespacewithimagelistview, container, false);
final ObservableListView listView = (ObservableListView) view.findViewById(R.id.scroll);
// Set padding view for ListView. This is the flexible space.
View paddingView = new View(getActivity());
final int flexibleSpaceImageHeight = getResources().getDimensionPixelSize(R.dimen.flexible_space_image_height);
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
flexibleSpaceImageHeight);
paddingView.setLayoutParams(lp);
// This is required to disable header's list selector effect
paddingView.setClickable(true);
listView.addHeaderView(paddingView);
setDummyData(listView);
// TouchInterceptionViewGroup should be a parent view other than ViewPager.
// This is a workaround for the issue #117:
// https://github.com/ksoichiro/Android-ObservableScrollView/issues/117
listView.setTouchInterceptionViewGroup((ViewGroup) view.findViewById(R.id.fragment_root));
// Scroll to the specified offset after layout
Bundle args = getArguments();
if (args != null && args.containsKey(ARG_SCROLL_Y)) {
final int scrollY = args.getInt(ARG_SCROLL_Y, 0);
ScrollUtils.addOnGlobalLayoutListener(listView, new Runnable() {
@SuppressLint("NewApi")
@Override
public void run() {
int offset = scrollY % flexibleSpaceImageHeight;
listView.setSelectionFromTop(0, -offset);
}
});
updateFlexibleSpace(scrollY, view);
} else {
updateFlexibleSpace(0, view);
}
listView.setScrollViewCallbacks(this);
updateFlexibleSpace(0, view);
return view;
}
@Override
public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
final WindowManager.LayoutParams wparams
= (WindowManager.LayoutParams) params;
view.setLayoutParams(wparams);
}
public GoogleCircleHookRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View v = LayoutInflater.from(context).inflate(R.layout.layout_google_refresh,null);
addView(v);
FrameLayout.LayoutParams params = (LayoutParams) v.getLayoutParams();
params.height = (int) getResources().getDimension(R.dimen.refresh_height_google);
v.setLayoutParams(params);
}