创建特定的布局 - Android

时间:2014-09-24 15:08:21

标签: android android-layout android-listview android-gridview

如何创建此特定布局

Layout 我需要GridView有两个列,但第一行应该是这样的。如何更改GridView的第一行。或者还有另一种解决方案。

2 个答案:

答案 0 :(得分:1)

试试这段代码: -

创建DemoActivity.java

public class DemoActivity extends Activity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView listView = (ListView) findViewById(R.id.listView1);

    View header = View.inflate(this, R.layout.custom_header1, null);
    listView.addHeaderView(header);

    MySimpleAdapter adapter = new MySimpleAdapter(DemoActivity.this );
    listView.setAdapter(adapter);

    }}

创建MySimpleAdapter.java

public class MySimpleAdapterextends BaseAdapter{
private  Context context;


public MySimpleAdapter(Context context) {
    super();
    this.context = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.custom_header2, parent, false);
    return rowView;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 10;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}} 

创建main.xml: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

创建custom_header1.xml: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"

    android:src="@android:drawable/sym_def_app_icon" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:src="@android:drawable/sym_def_app_icon" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_below="@+id/imageView2"
    android:src="@android:drawable/sym_def_app_icon" />

创建custom_header2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@drawable/ic_launcher" />

答案 1 :(得分:0)

如果第一行看起来不像网格的其余部分,我只需将它作为一个自己的布局元素添加到网格顶部,就像标题一样。