我是Android编程的新手,我正在尝试将按钮放入垂直滚动网格中。按钮不起作用或不可见。当我启用"显示布局边界"在开发人员选项中,您会看到gridview为按钮::
分配空间我的代码如下:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView1"
android:scrollbars="vertical"
android:numColumns="4"
/>
</LinearLayout>
片段:
import android.app.ActionBar;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fairphone.datatransparency.R;
import java.util.Iterator;
/**
* Created by koen on 11/25/15.
*/
public class NewTimeLineFragment extends Fragment{
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(
R.layout.new_time_line_layout, container, false);
Bundle args = getArguments();
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT);
Button[] buttons = new Button[6];
View.OnClickListener oclBtn = new View.OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "GORGABAL", 5).show();
}
};
for(int i = 0; i < buttons.length; i++){
buttons[i] = new Button(getActivity());
buttons[i].setBackgroundColor(Color.BLACK);
buttons[i].setVisibility(View.VISIBLE);
buttons[i].setOnClickListener(oclBtn);
buttons[i].setLayoutParams(lp);
buttons[i].setText("GORGABAL");
}
GridView gridview;
gridview = (GridView) rootView.findViewById(R.id.gridView1);
ArrayAdapter<Button> adapter = new ArrayAdapter<Button>(this.getActivity(), android.R.layout.simple_list_item_1, buttons);
gridview.setAdapter(adapter);
gridview.setVisibility(View.VISIBLE);
Log.d("Debug", "new_time_line fragment created");
return rootView;
}
}