下面列出了android.widget.LinearLayout#getChildCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public KeyboardToolPop(Context context, OnClickListener onClickListener) {
super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
@SuppressLint("InflateParams")
View view = LayoutInflater.from(context).inflate(R.layout.pop_soft_keyboard_top_tool, null);
this.setContentView(view);
setTouchable(true);
setOutsideTouchable(false);
setFocusable(false);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); //解决遮盖输入法
LinearLayout linearLayout = getContentView().findViewById(R.id.ll_content);
for (int i = 0; i < linearLayout.getChildCount(); i++) {
TextView tv = (TextView) linearLayout.getChildAt(i);
tv.setOnClickListener(v -> {
if (onClickListener != null) {
onClickListener.click(((TextView) v).getText().toString());
}
});
}
}
/**
* Recycles items from specified layout.
* There are saved only items not included to specified range.
* All the cached items are removed from original layout.
*
* @param layout the layout containing items to be cached
* @param firstItem the number of first item in layout
* @param range the range of current wheel items
* @return the new value of first item number
*/
public int recycleItems(LinearLayout layout, int firstItem, ItemsRange range) {
int index = firstItem;
for (int i = 0; i < layout.getChildCount();) {
if (!range.contains(index)) {
recycleView(layout.getChildAt(i), index);
layout.removeViewAt(i);
if (i == 0) { // first item
firstItem++;
}
} else {
i++; // go to next item
}
index++;
}
return firstItem;
}
public void setPopupItemsColor(int color, boolean icon) {
if (popupLayout == null) {
return;
}
final LinearLayout layout = popupLayout.linearLayout;
for (int a = 0, count = layout.getChildCount(); a < count; a++) {
final View child = layout.getChildAt(a);
if (child instanceof TextView) {
((TextView) child).setTextColor(color);
} else if (child instanceof ActionBarMenuSubItem) {
if (icon) {
((ActionBarMenuSubItem) child).setIconColor(color);
} else {
((ActionBarMenuSubItem) child).setTextColor(color);
}
}
}
}
public int getPositionInListX(int x,LinearLayout layout){
for(int i = 0; i < layout.getChildCount();i++){
int[] location = new int[2];
View view = layout.getChildAt(i);
int end = layout.getWidth();
if(layout.getChildCount() > i+1){
int[] end_location = new int[2];
layout.getChildAt(i+1).getLocationOnScreen(end_location);
end = end_location[0];
}
view.getLocationOnScreen(location);
if(x >= location[0] && x <= end){
return i;
}
}
return 0;
}
/**
* Recycles items from specified layout.
* There are saved only items not included to specified range.
* All the cached items are removed from original layout.
*
* @param layout the layout containing items to be cached
* @param firstItem the number of first item in layout
* @param range the range of current wheel items
* @return the new value of first item number
*/
public int recycleItems(LinearLayout layout, int firstItem, ItemsRange range) {
int index = firstItem;
for (int i = 0; i < layout.getChildCount();) {
if (!range.contains(index)) {
recycleView(layout.getChildAt(i), index);
layout.removeViewAt(i);
if (i == 0) { // first item
firstItem++;
}
} else {
i++;
}
index++;
}
return firstItem;
}
public static void setFocusParms(LinearLayout currentRow, LinearLayout otherRow, boolean up) {
for (int currentRowNdx = 0; currentRowNdx < currentRow.getChildCount(); currentRowNdx++) {
ProgramGridCell cell = (ProgramGridCell) currentRow.getChildAt(currentRowNdx);
ProgramGridCell otherCell = getOtherCell(otherRow, cell);
if (otherCell != null) {
if (up) {
cell.setNextFocusUpId(otherCell.getId());
//TvApp.getApplication().getLogger().Debug("Setting up focus for " + cell.getProgram().getName() + " to " + otherCell.getProgram().getName()+"("+otherCell.getId()+")");
} else {
cell.setNextFocusDownId(otherCell.getId());
//TvApp.getApplication().getLogger().Debug("Setting down focus for " + cell.getProgram().getName() + " to " + otherCell.getProgram().getName());
}
}
}
}
/**
* view转bitmap
*/
private static Bitmap viewConversionBitmap(View v) {
int w = v.getWidth();
int h = 0;
if (v instanceof LinearLayout){
LinearLayout linearLayout = (LinearLayout) v;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
h += linearLayout.getChildAt(i).getHeight();
}
} else if (v instanceof RelativeLayout){
RelativeLayout relativeLayout = (RelativeLayout) v;
for (int i = 0; i < relativeLayout.getChildCount(); i++) {
h += relativeLayout.getChildAt(i).getHeight();
}
} else {
h = v.getHeight();
}
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
//如果不设置canvas画布为白色,则生成透明
c.drawColor(Color.WHITE);
v.layout(0, 0, w, h);
v.draw(c);
return bmp;
}
/**
* This procedure performs recursive initialization of elements from included layouts
*/
private void initializeLayout(LinearLayout l)
{
for (int k = 0; k < l.getChildCount(); k++)
{
View v = l.getChildAt(k);
if (v instanceof CustomTextView)
{
initializeSymbol((CustomTextView) v);
}
if (v instanceof CustomEditText)
{
initializeTerm((CustomEditText) v, l);
}
if (v instanceof LinearLayout)
{
initializeLayout((LinearLayout) v);
}
}
}
/**
* @param giftLayoutParent 存放礼物控件的父容器
* @param giftLayoutNums 礼物控件的数量
* @return
*/
public GiftControl setGiftLayout(LinearLayout giftLayoutParent, @NonNull int giftLayoutNums) {
if (giftLayoutNums <= 0) {
throw new IllegalArgumentException("GiftFrameLayout数量必须大于0");
}
if (giftLayoutParent.getChildCount() > 0) {//如果父容器没有子孩子,就进行添加
return this;
}
mGiftLayoutParent = giftLayoutParent;
mGiftLayoutMaxNums = giftLayoutNums;
LayoutTransition transition = new LayoutTransition();
transition.setAnimator(LayoutTransition.CHANGE_APPEARING,
transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
transition.setAnimator(LayoutTransition.APPEARING,
transition.getAnimator(LayoutTransition.APPEARING));
transition.setAnimator(LayoutTransition.DISAPPEARING,
transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,
transition.getAnimator(LayoutTransition.DISAPPEARING));
mGiftLayoutParent.setLayoutTransition(transition);
return this;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 显示的设置一个宽度
*/
if (!once) {
LinearLayout wrapper = (LinearLayout) getChildAt(0);
mMenu = (ViewGroup) wrapper.getChildAt(0);
mMenuWidth = mScreenWidth - mMenuRightPadding;
mHalfMenuWidth = mMenuWidth / 2;
mMenu.getLayoutParams().width = mMenuWidth;
for (int i = 1; i < wrapper.getChildCount(); i++) {
ViewGroup mContent = (ViewGroup) wrapper.getChildAt(i);
mContent.getLayoutParams().width = mScreenWidth;
}
}
updateState();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setColorPreview(LinearLayout colorPreview, Integer selectedColor) {
if (colorPreview == null)
return;
this.colorPreview = colorPreview;
if (selectedColor == null)
selectedColor = 0;
int children = colorPreview.getChildCount();
if (children == 0 || colorPreview.getVisibility() != View.VISIBLE)
return;
for (int i = 0; i < children; i++) {
View childView = colorPreview.getChildAt(i);
if (!(childView instanceof LinearLayout))
continue;
LinearLayout childLayout = (LinearLayout) childView;
if (i == selectedColor) {
childLayout.setBackgroundColor(Color.WHITE);
}
ImageView childImage = (ImageView) childLayout.findViewById(R.id.image_preview);
childImage.setClickable(true);
childImage.setTag(i);
childImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v == null)
return;
Object tag = v.getTag();
if (tag == null || !(tag instanceof Integer))
return;
setSelectedColor((int) tag);
}
});
}
}
/**
* 把系统自身请求失败时的网页隐藏
*/
protected void hideErrorPage() {
LinearLayout webParentView = (LinearLayout) mWebView.getParent();
mIsErrorPage = false;
while (webParentView.getChildCount() > 1) {
webParentView.removeViewAt(0);
}
}
private void enableAllChildren(LinearLayout parent, boolean b) {
try {
for (int i = 0; i < parent.getChildCount(); i++) {
View v = parent.getChildAt(i);
v.setSelected(b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Update a existing view with some column.
*
* @param position Current position of the cell.
* @param viewGroup Container of the cell.
* @param layout Layout saved of the cell.
*/
private void updateItemRow(int position, ViewGroup viewGroup, LinearLayout layout) {
// we remove view that aren't needed for the row
while(layout.getChildCount()>rows.get(position).size()){
layout.removeViewAt(layout.getChildCount()-1);
}
for (int i = 0; i < rows.get(position).size(); i++) {
int currentPos = rows.get(position).indexFirstItem + i;
View insideView = layout.getChildAt(i);
View theView = super.getView(currentPos, insideView, viewGroup);
// Set the width of this column
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = rows.get(position).get(i).colSpan;
if (i != mNumColumns - 1)// last col
params.setMargins(0, 0, cellMargin, 0);
theView.setLayoutParams(params);
theView.setOnClickListener(new ListItemClickListener(currentPos));
if (!theView.equals(insideView)) {
if (layout.getChildCount() > i) {
layout.removeViewAt(i);
}
layout.addView(theView, i);
}
}
}
public static Integer findItemIndex(ODrawerItem item, LinearLayout itemContainer) {
for (int i = 0; i < itemContainer.getChildCount(); i++) {
ODrawerItem dItem = (ODrawerItem) itemContainer.getChildAt(i).getTag();
if (dItem != null && dItem.getKey().equals(item.getKey())) {
return i;
}
}
return -1;
}
public void setColorPreview(LinearLayout colorPreview, Integer selectedColor) {
if (colorPreview == null)
return;
this.colorPreview = colorPreview;
if (selectedColor == null)
selectedColor = 0;
int children = colorPreview.getChildCount();
if (children == 0 || colorPreview.getVisibility() != View.VISIBLE)
return;
for (int i = 0; i < children; i++) {
View childView = colorPreview.getChildAt(i);
if (!(childView instanceof LinearLayout))
continue;
LinearLayout childLayout = (LinearLayout) childView;
if (i == selectedColor) {
childLayout.setBackgroundColor(Color.WHITE);
}
ImageView childImage = (ImageView) childLayout.findViewById(R.id.image_preview);
childImage.setClickable(true);
childImage.setTag(i);
childImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v == null)
return;
Object tag = v.getTag();
if (tag == null || !(tag instanceof Integer))
return;
setSelectedColor((int) tag);
}
});
}
}
private void init(){
Context context=getContext();
View view=View.inflate(context, R.layout.markdown_edittext,this);
editText=(AppCompatEditText)view.findViewById(R.id.markdown_edit);
LinearLayout linearLayout=(LinearLayout) view.findViewById(R.id.span_toolbar);
for(int i=0;i<linearLayout.getChildCount();i++){
linearLayout.getChildAt(i).setOnClickListener(this);
}
editText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
if (KeyEvent.ACTION_DOWN == event.getAction()) {
int index = getParagraphStart();
String text = editText.getText().toString().substring(index);
int index2 = text.indexOf("- ");
if (index2 >= 0 && text.substring(0, index2).trim().isEmpty()) {
if (editText.getSelectionStart() < editText.getSelectionEnd()) {
editText.getEditableText().replace(editText.getSelectionStart(), editText.getSelectionEnd(), "\n" + text.substring(0, index2) + "- ");
} else {
editText.getEditableText().insert(editText.getSelectionStart(), "\n" + text.substring(0, index2) + "- ");
}
return true;
}
}
break;
}
return false;
}
});
}
public void removeBarValues() {
if (oldFrameLayout != null)
removeClickedBar();
final int barsCount = ((LinearLayout) this.getChildAt(0)).getChildCount();
for (int i = 0; i < barsCount; i++) {
FrameLayout rootFrame = (FrameLayout) ((LinearLayout) this.getChildAt(0)).getChildAt(i);
int rootChildCount = rootFrame.getChildCount();
for (int j = 0; j < rootChildCount; j++) {
View childView = rootFrame.getChildAt(j);
if (childView instanceof LinearLayout) {
//bar
LinearLayout barContainerLinear = ((LinearLayout) childView);
int barContainerCount = barContainerLinear.getChildCount();
for (int k = 0; k < barContainerCount; k++) {
View view = barContainerLinear.getChildAt(k);
if (view instanceof Bar) {
BarAnimation anim = new BarAnimation(((Bar) view), (int) (mDataList.get(i).getBarValue() * 100), 0);
anim.setDuration(250);
((Bar) view).startAnimation(anim);
}
}
}
}
}
isBarsEmpty = true;
}
private void performOK() {
PreferencesFragment fragment = (PreferencesFragment) getTargetFragment();
if (fragment == null) {
return;
}
if (validate()) {
Editable editable = mKey.getText();
String key = "";
if (editable != null) {
key = editable.toString();
}
Object value = null;
switch (mPreferenceType) {
case BOOLEAN:
value = ((CompoundButton) mValue).isChecked();
break;
case INT:
value = Integer.valueOf(((EditText) mValue).getText().toString());
break;
case STRING:
value = ((EditText) mValue).getText().toString();
break;
case FLOAT:
value = Float.valueOf(((EditText) mValue).getText().toString());
break;
case LONG:
value = Long.valueOf(((EditText) mValue).getText().toString());
break;
case STRINGSET:
Set<String> set = new HashSet<String>();
LinearLayout container = (LinearLayout) mValue;
for (int i = 0; i < container.getChildCount(); i++) {
set.add(((EditText) ((ViewGroup) ((ViewGroup) container.getChildAt(i)).getChildAt(0)).getChildAt(1)).getText().toString());
}
value = set;
break;
case UNSUPPORTED:
break;
}
fragment.addPrefKeyValue(mEditKey, key, value, mEditMode);
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
((FDroidApp) getApplication()).applyTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MainViewAdapter(this);
pager = (RecyclerView) findViewById(R.id.main_view_pager);
pager.setHasFixedSize(true);
pager.setLayoutManager(new NonScrollingHorizontalLayoutManager(this));
pager.setAdapter(adapter);
// Without this, the focus is completely busted on pre 15 devices. Trying to use them
// without this ends up with each child view showing for a fraction of a second, then
// reverting back to the "Latest" screen again, in completely non-deterministic ways.
if (Build.VERSION.SDK_INT <= 15) {
pager.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
updatesBadge = new TextBadgeItem().hide(false);
bottomNavigation = (BottomNavigationBar) findViewById(R.id.bottom_navigation);
bottomNavigation
.addItem(new BottomNavigationItem(R.drawable.ic_latest, R.string.main_menu__latest_apps));
if (BuildConfig.FLAVOR.startsWith("full")) {
bottomNavigation
.addItem(new BottomNavigationItem(R.drawable.ic_categories, R.string.main_menu__categories))
.addItem(new BottomNavigationItem(R.drawable.ic_nearby, R.string.main_menu__swap_nearby));
bottomNavigation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomNavigation.getCurrentSelectedPosition() == 2) {
NearbyViewBinder.updateUsbOtg(MainActivity.this);
}
}
});
}
bottomNavigation.setTabSelectedListener(this)
.setBarBackgroundColor(getBottomNavigationBackgroundColorResId())
.setInActiveColor(R.color.bottom_nav_items)
.setActiveColor(android.R.color.white)
.setMode(BottomNavigationBar.MODE_FIXED)
.addItem(new BottomNavigationItem(R.drawable.ic_updates, R.string.main_menu__updates)
.setBadgeItem(updatesBadge))
.addItem(new BottomNavigationItem(R.drawable.ic_settings, R.string.menu_settings))
.setAnimationDuration(0)
.initialise();
// turn off animation, scaling, and truncate labels in the middle
final LinearLayout linearLayout = bottomNavigation.findViewById(R.id.bottom_navigation_bar_item_container);
final int childCount = linearLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
final View fixedBottomNavigationTab = linearLayout.getChildAt(i);
try {
Field labelScale = fixedBottomNavigationTab.getClass().getDeclaredField("labelScale");
labelScale.setAccessible(true);
labelScale.set(fixedBottomNavigationTab, 1.0f);
} catch (IllegalAccessException | NoSuchFieldException | IllegalArgumentException e) {
e.printStackTrace();
}
final View container = fixedBottomNavigationTab.findViewById(R.id.fixed_bottom_navigation_container);
container.setPadding(
2,
container.getPaddingTop(),
2,
container.getPaddingBottom()
);
final TextView title = fixedBottomNavigationTab.findViewById(R.id.fixed_bottom_navigation_title);
title.setEllipsize(TextUtils.TruncateAt.MIDDLE);
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
}
IntentFilter updateableAppsFilter = new IntentFilter(AppUpdateStatusManager.BROADCAST_APPSTATUS_LIST_CHANGED);
updateableAppsFilter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_CHANGED);
updateableAppsFilter.addAction(AppUpdateStatusManager.BROADCAST_APPSTATUS_REMOVED);
LocalBroadcastManager.getInstance(this).registerReceiver(onUpdateableAppsChanged, updateableAppsFilter);
if (savedInstanceState != null) {
selectedMenuId = savedInstanceState.getInt(STATE_SELECTED_MENU_ID, (int) adapter.getItemId(0));
} else {
selectedMenuId = (int) adapter.getItemId(0);
}
setSelectedMenuInNav();
initialRepoUpdateIfRequired();
Intent intent = getIntent();
handleSearchOrAppViewIntent(intent);
}