2015-10-07 35 views
1

我有mainactivitygridimageactivity我注意到gridimageactivity正在膨脹的主要活動佈局。我想在R.main_activity中添加新的按鈕和事物。如何解決通貨膨脹另一個R.layout

這是主要活動,因爲你注意到我評論了佈局,但該應用程序仍然顯示textviews和圖像。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    // setContentView(R.layout.activity_main); 
     // list = (ListView) findViewById(R.id.list); 
     Listitem = new ArrayList<Listitem>(); 
     mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 

     GetDataJSON gj = new GetDataJSON(); 
     gj.execute(); 
     mProgressBar.setVisibility(View.VISIBLE); 

這是gridadapter我猜是什麼原因造成的問題

LayoutInflater inflater = LayoutInflater.from(mcontext); 
row = inflater.inflate(layoutResourceId, parent, false); 
holder = new ViewHolder(); 

我打電話gridimage這樣 GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);

我的問題:我希望能夠膨脹我mainactivity以添加工具欄然而在gridview中的膨脹阻止了我。如何fx的?

public class GridViewAdapter extends ArrayAdapter<GridImages> { 

    private Context mContext; 
    private int layoutResourceId; 
    private ArrayList<GridImages> mGridImages = new ArrayList<GridImages>(); 



    public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridImages> mGridImages) { 
     super(mContext, layoutResourceId, mGridImages); 
     this.layoutResourceId = layoutResourceId; 
     this.mContext = mContext; 
     this.mGridImages = mGridImages; 
    } 


    /** 
    * Updates grid data and refresh grid items. 
    * @param mGridData 
    */ 
    public void setGridData(ArrayList<GridImages> mGridImages) { 
     this.mGridImages = mGridImages; 
    } 
+0

添加按鈕'公共類GridViewActivity擴展AppCompatActivity { @覆蓋 保護無效onCreate(Bundle savedInstanceState) {super_onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout layout =(RelativeLayout)findViewById(R.id.relativeLayout); Button btnTag = new Button(this); btnTag.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT)); btnTag.setText(「DynAdded Button」); layout.addView(btnTag); } }' – BNK

+0

添加爲答案它會更清晰,我和病態upvote你 – Moudiz

回答

1

正如評論,請嘗試以下(在GridViewActivity中添加一個使用layout_main.xml的按鈕示例)。

public class GridViewActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout); 
     Button btnTag = new Button(this); 
     btnTag.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 
     btnTag.setText("Dynamic Added Button"); 
     layout.addView(btnTag); 
    } 
} 

但是,您應該檢查它是否適用於您的項目。

UPDATE:

row_grid.xml:

我在你前面的問題的答案:

customGridAdapter = new CustomGridViewAdapter(mContext, R.layout.row_grid, gridArray); 
+0

我添加了我的gridview活動我已經擴展arrayadapter。 。我添加oncreate方法?按鈕如何工作? – Moudiz

+0

對不起,你能澄清一下嗎?我不明白你的意見 – BNK

+0

檢查我的編輯請添加我的GridViewAdapter類。在類中我擴展'擴展ArrayAdapter '你怎麼在你的例子中說'擴展AppCompatActivity'然後怎麼做? – Moudiz

相關問題