2016-08-05 58 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

我更新了佈局。請檢查。 –

相關問題