ViewGroup parent = (ViewGroup) findViewById(R.id.parent); View C = getLayoutInflater().inflate(optionId, parent, false); parent.addView(C, index);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.your_layout, null); // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.a_text_view); textView.setText("your text"); // insert into main view ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point); insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
int placeholderId = R.id.placeholder; // placeholderId==12 // returns *placeholder* which has id==12: ViewGroup placeholder = (ViewGroup)this.findViewById(placeholderId); for (int i=0; i<20; i++){ TextView tv = new TextView(this.getApplicationContext()); // One new TextView will also be assigned an id==12: tv.setId(i); placeholder.addView(tv); }
public void addTextureView() { Log.d(TAG, "addTextureView [" + this.hashCode() + "] "); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER); textureViewContainer.addView(JZMediaManager.textureView, layoutParams); }
/** * show的时候调用 * * @param view 这个View */ private void onAttached(View view) { mPickerOptions.decorView.addView(view); if (isAnim) { contentContainer.startAnimation(inAnim); } }
@Override public View instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(container.getContext()); photoView.setImageResource(sDrawables[position]); // Now just add PhotoView to ViewPager and return it container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return photoView; }
int optionId = someExpression ? R.layout.option1 : R.layout.option2; View C = findViewById(R.id.C); ViewGroup parent = (ViewGroup) C.getParent(); int index = parent.indexOfChild(C); parent.removeView(C); C = getLayoutInflater().inflate(optionId, parent, false); parent.addView(C, index);
/** * 添加到全屏父布局里 */ private void resolveFullAdd() { fullViewContainer.setBackgroundColor(Color.BLACK); fullViewContainer.addView(gsyVideoPlayer); resolveChangeFirstLogic(50); }
void removeFromTab(BottomBarTab tab) { BadgeContainer badgeAndTabContainer = (BadgeContainer) getParent(); ViewGroup originalTabContainer = (ViewGroup) badgeAndTabContainer.getParent(); badgeAndTabContainer.removeView(tab); originalTabContainer.removeView(badgeAndTabContainer); originalTabContainer.addView(tab, tab.getIndexInTabContainer()); }
@Test public void testAddViewAt() throws Exception { root.removeAllViews(); root.addView(child1); root.addView(child2); root.addView(child3, 1); assertThat(root.getChildAt(0)).isSameAs(child1); assertThat(root.getChildAt(1)).isSameAs((View) child3); assertThat(root.getChildAt(2)).isSameAs(child2); }
@Test public void shouldNotFindViewWithTagReturnNull() { root.removeAllViews(); child1.setTag("tag1"); child2.setTag("tag2"); child3.setTag("tag3"); root.addView(child1); root.addView(child2); root.addView(child3, 1); assertThat((View) root.findViewWithTag("tag21")).isNull(); assertThat((ViewGroup) root.findViewWithTag("tag23")).isNull(); }
@Test public void addViewWithLayoutParams_shouldStoreLayoutParams() throws Exception { FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); View child1 = new View(ApplicationProvider.getApplicationContext()); View child2 = new View(ApplicationProvider.getApplicationContext()); root.addView(child1, layoutParams1); root.addView(child2, 1, layoutParams2); assertSame(layoutParams1, child1.getLayoutParams()); assertSame(layoutParams2, child2.getLayoutParams()); }
@Test public void addView_whenChildAlreadyHasAParent_shouldThrow() throws Exception { ViewGroup newRoot = new FrameLayout(context); try { newRoot.addView(child1); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { // pass } }
@Test(expected = RuntimeException.class) public void checkedClick_shouldThrowIfViewIsNotVisible() throws Exception { ViewGroup grandParent = new LinearLayout(context); ViewGroup parent = new LinearLayout(context); grandParent.addView(parent); parent.addView(view); grandParent.setVisibility(View.GONE); shadowOf(view).checkedPerformClick(); }
@Test public void addView_withIndex_shouldRequestLayout() throws Exception { View view = new View(context); ViewGroup viewGroup = new FrameLayout(context); viewGroup.addView(view, 0); assertThat(shadowOf(viewGroup).didRequestLayout()).isTrue(); }
@Test public void addView_shouldRequestLayout() throws Exception { View view = new View(context); ViewGroup viewGroup = new FrameLayout(context); viewGroup.addView(view); assertThat(shadowOf(viewGroup).didRequestLayout()).isTrue(); }
@Test public void removeAllViews_shouldCallOnChildViewRemovedWithEachChild() throws Exception { View view = new View(context); ViewGroup viewGroup = new FrameLayout(context); viewGroup.addView(view); TestOnHierarchyChangeListener testListener = new TestOnHierarchyChangeListener(); viewGroup.setOnHierarchyChangeListener(testListener); viewGroup.removeAllViews(); assertTrue(testListener.wasCalled()); }
@Test public void removeAllViews_shouldRequestLayout() throws Exception { View view = new View(context); ViewGroup viewGroup = new FrameLayout(context); viewGroup.addView(view); shadowOf(viewGroup).setDidRequestLayout(false); viewGroup.removeAllViews(); assertThat(shadowOf(viewGroup).didRequestLayout()).isTrue(); }
@Test public void removeView_shouldRequestLayout() throws Exception { View view = new View(context); ViewGroup viewGroup = new FrameLayout(context); viewGroup.addView(view); shadowOf(viewGroup).setDidRequestLayout(false); viewGroup.removeView(view); assertThat(shadowOf(viewGroup).didRequestLayout()).isTrue(); }
@Test public void removeViewAt_shouldRequestLayout() throws Exception { View view = new View(context); ViewGroup viewGroup = new FrameLayout(context); viewGroup.addView(view); shadowOf(viewGroup).setDidRequestLayout(false); viewGroup.removeViewAt(0); assertThat(shadowOf(viewGroup).didRequestLayout()).isTrue(); }