2016-08-05 60 views
0

我已经创建了width = 3和height = 2的网格布局。我在每个网格元素中放置6个视图,id为view1,view2,view3,view4,view5和view 6.现在如何以编程方式在每个视图上放置按钮?如何在我的自定义视图中放置按钮?

View child1,child2,child3,child4,child5,child6; 
    child1=(View)view.findViewById(R.id.view1); 
    child2=(View)view.findViewById(R.id.view2); 
    child3=(View)view.findViewById(R.id.view3); 
    child4=(View)view.findViewById(R.id.view4); 
    child5=(View)view.findViewById(R.id.view5); 
    child6=(View)view.findViewById(R.id.view6); 

并最终放置动态按钮我使用的代码如下。

child1 = this.getActivity().getLayoutInflater().inflate(R.layout.video,null); 
         video = (Button)child1.findViewById(R.id.button); 

这继续为child2.3等.video是一个xml布局与按钮id作为按钮。

布局如下

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="1"> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#484848" 
     android:layout_weight=".1"> 
     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imb1" 
      android:layout_weight=".07" 
      android:layout_marginBottom="7.5dp" 
      android:textColor="#ffffff" 
      android:src="@drawable/back" 
      android:adjustViewBounds="false" 
      android:background="#484848" /> 
     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_gravity="center_vertical" 
      android:background="#484848" 
      android:src="@drawable/medilearn1" 
      android:layout_weight=".15"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Dashboard" 
      android:id="@+id/textView3" 
      android:layout_gravity="center" 
      android:textColor="#fbfbfb" 
      android:textAlignment="viewStart" 
      android:layout_marginLeft="20dp" 
      android:layout_weight="0.67" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Restart" 
      android:id="@+id/button3" 
      android:layout_weight=".07" 
      android:background="#d86018" 
      android:textColor="#ffffff" 
      android:layout_marginTop="7.5dp" 
      android:layout_marginBottom="7.5dp" 
      android:layout_marginRight="7.5dp" /> 

    </LinearLayout> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#d8d8d8" 
    android:layout_weight=".9"> 
    <SearchView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/searchView" 
     android:background="#ffffff" 
     android:queryHint="search" 
     android:layout_gravity="center_horizontal" /> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:background="#d8d8d8"> 
    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnCount="3" 
     android:rowCount="2" 
     android:id="@+id/gridView"> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="200dp" 
      android:layout_columnWeight="1" 
      android:id="@+id/view1" /> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="200dp" 
      android:layout_columnWeight="1" 
      android:id="@+id/view2" /> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="200dp" 
      android:layout_columnWeight="1" 
      android:id="@+id/view3" /> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="200dp" 
      android:layout_columnWeight="1" 
      android:id="@+id/view4" /> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="200dp" 
      android:layout_columnWeight="1" 
      android:id="@+id/view5" /> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="200dp" 
      android:layout_columnWeight="1" 
      android:id="@+id/view6" /> 

    </GridLayout> 
</ScrollView> 
</LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#484848" 
     android:layout_weight=".1"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Version:1.0" 
      android:id="@+id/textView6" 
      android:layout_gravity="center" 
      android:textColor="#fbfbfb" 
      android:textAlignment="viewStart" 
      android:layout_marginLeft="20dp" /> 

    </LinearLayout> 


    </LinearLayout> 

这是我的布局。

我找到了解决方案。我在HIGHT和宽度我希望每个网格宣布的LinearLayout,并呼吁孩子addView(),因此,

LinearLayout lay1=(LinearLayout)view.findViewById(R.id.layout1); 

然后

lay1.addView(child1); 
+0

使与arrayAdapter和独立的GridView行的XML定制的GridView – Aditi

+0

对不起,我不能understand.canü请给我一个例子。@阿迪提 –

回答

0

您可以将按钮添加到您的看法是这样的:

Button btn = new Button(); 
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
btn.setId(1); 
btn.setText("ButtonText"); 

yourView.addView(btn); 

如果你想它来填充你的整个观点,与LayoutParams.MATCH_PARENT更换LayoutParams.WRAP_CONTENT

+0

不,这不是我想要的。 –

0

为你的GridView

MyGridAdapter csAdapter = new MyGridAdapter(); 
    csGridView.setAdapter(csAdapter); 
    csGridView.setOnItemClickListener(gridItemClicked); 

和适配器代码只是设置适配器

public AdapterView.OnItemClickListener gridItemClicked = new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

//your code 

     } 
    }; 
0

更新: 根据您发布的布局有两种选择,你可以追求:

  1. 在你的xml文件中创建布局:你可以只需将View标签替换为Button标签,如果您的布局是静态的并且始终需要这些按钮。
  2. 动态创建布局:为此,您不需要在布局中使用View标签,因为这些标签将以编程方式创建,因此您可以删除这些标签。

    View child = this.getActivity().getLayoutInflater().inflate(R.layout.video,null); 
    GridLayout.LayoutParams params = new GridLayout.LayoutParams(); 
    // set the row number this child is located in 
    params.rowSpec = GridLayout.spec(rowNum, 1); 
    // set the column number this child is located in 
    params.columnSpec = GridLayout.spec(colNum, 1); 
    child.setLayoutParams(params); 
    grid.addView(child); 
    

而且,请注意,View不能有一个孩子View,只有:那您要创建(我假设包含的按钮布局叫做video.xml)每Button以下布局延伸ViewGroup(例如FrameLayout,GridLayout,...)可以有孩子View s!


夸大你的情况View最好的办法是将下面的代码:

GridLayout grid = view.findViewById(R.id.grid); // get the grid layout 
// ... 
child1 = this.getActivity().getLayoutInflater().inflate(R.layout.video, grid, true); 
video = (Button)child1.findViewById(R.id.button); 

通过提供父(grid),该LayoutInflator将采取适当的膨胀所请求的资源到护理视图层次结构,true将膨胀的布局附加到根布局。

同时检查API文档此:

inflate(int resource, ViewGroup root, boolean attachToRoot): 从指定的XML资源充气一个新的视图层次。

+0

它不工作。我想在3 * 2网格中放置动态按钮,在每种情况下,三个按钮应该填满整行。我使用了grid.addView(child),其中child是一个视图。但它并没有给我我想要的东西。所以我创建了6个自定义视图(每行3个),并且我想在其中放置按钮。如果你有更好的解决方案这个PLZ建议。@ TR4Android –

+0

你用什么作为你的根布局?一个'GridView','GridLayout'或者其他什么东西? – TR4Android

+0

我更新了布局。请检查。 –

相关问题