add view in code
2016-08-04 11:33
176 查看
标题 ##sample add view in code :
we could be realize it in xml like this :<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="1dp" android:layout_height="match_parent" android:padding="10dp" android:layout_centerInParent="true" android:background="@drawable/alert_content_line2" /> <GridView android:id="@+id/list" android:layout_width="match_parent" android:padding="10dp" android:layout_height="match_parent" android:numColumns="2" /> </RelativeLayout>
but now we also can realize it with code in java
RelativeLayout relativeLayout = new RelativeLayout(context); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); View v = new View(context); v.setBackgroundResource(R.drawable.alert_content_line1); relativeLayout.addView(v,params); LayoutParams layoutParams = new LayoutParams(1,-1); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); layoutParams.setMargins(0, 10, 0, 10); v.setLayoutParams(layoutParams); mGridView = new GridView(context); mGridView.setNumColumns(2); relativeLayout.addView(mGridView,params); LayoutParams layoutParams2 = new LayoutParams(-1,-1); layoutParams2.setMargins(10, 0, 10, 0); mGridView.setLayoutParams(layoutParams2);
相关文章推荐
- 欢迎使用 FreeCode for VS60/VS2005 Add-in
- 欢迎使用 FreeCode for VS60/VS2005 Add-in
- Call to a member function addToChildGroup() on a non-object in app/code/core/Mage/Core/Block/Abstrac
- Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Code view is missing in SharePoint Designer Beta 2 when you try to edit a WSS v3 site.
- I have a custom view that I want to be able to initialize both in-code and in nib.
- Annotations in Tiger, Part 1: Add metadata to Java code
- View the android source code in Eclipse
- Multiple Models in a View in ASP.NET MVC 4 / MVC 5 (with source code)
- use MyButton is simple,only add two file and then add some code just bellow in any dlg src declear the button control and will ok!
- Code Project Browser Add-In for VS
- 欢迎使用 FreeCode for VS60/VS2005 Add-in
- [Drupal] How to add the js file and js code block in Drupal
- View android source code in eclipse
- 欢迎使用 FreeCode for VS60/VS2005 Add-in
- 欢迎使用 FreeCode for VS60/VS2005 Add-in
- View android source code in Eclipse
- How To Add Search Bar in Table View
- addView(View, LayoutParams) is not supported in AdapterView