使用GridView旋转的布局问题

时间:2014-11-02 17:44:57

标签: android gridview layout

我的目标是有一个屏幕,其顶部为textView,底部为按钮,中间部分的作用类似于水平滚动网格。 使用带有旋转270的gridView的片段,并旋转每个项目90使我接近我正在尝试实现的...但是我正在努力解决这些布局问题,你可以在图片中看到:

enter image description here

1)改变片段的高度将导致第1行项目被部分剪裁

2)gridView的宽度会根据需要向右滚动,但gridView的可见右边缘不对齐到屏幕的右边缘

3)我想调整网格的顶行和底行之间的空间,我希望通过改变gridView的高度来做到这一点,但是调整gridView或片段的高度也会改变宽度

4)使用layout_weight与0dp设置为高度的典型组合不起作用,因为片段不会将其高度调整为所述重量...也会缩小宽度。要访问附加图片中的视图,我必须在activity_my.xml

中的dp中添加固定高度

这是我的代码......

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <TextView
        android:id="@+id/top_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        android:lines="1"
        android:textSize="17sp"
        android:text="First text block"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_weight="0.50" />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:name="com.example.horizontalgrid2.MAFragment"
        android:id="@+id/fragment"
        tools:layout="@layout/fragment_ma"
        android:background="#ff8c8c00"
        android:layout_weight="0.35"/>

    <Button
        android:id="@+id/button_text"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginTop="0dp"
        android:textSize="17sp"
        android:text="Second text block"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_weight="0.15"/>

</LinearLayout>

fragment_ma.xml

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

    <GridView
        android:id="@+id/tablegrid"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp"
        android:rotation="270"
        android:numColumns="2"
        />
</LinearLayout>

item_caller_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:rotation="90">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/grid_item_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            >
        </ImageView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Name"
            android:layout_below="@+id/grid_item_image"/>

    </RelativeLayout>
</LinearLayout>

MyActivity.java

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

    }
}

MAFragment.java

public class MAFragment extends Fragment {

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_ma, container, false);
        GridView gridview = (GridView) view.findViewById(R.id.tablegrid);
        gridview.setAdapter(new ImageAdapter(getActivity()));

        return view;
    }

}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(mContext);

            gridView = inflater.inflate(R.layout.item_caller_grid, null);

            // set image based on selected text
            imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);


            imageView.setImageResource(mThumbIds[position]);


        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher
    };
}

0 个答案:

没有答案