高程未在GridView中显示材质设计

时间:2017-12-25 14:30:50

标签: java android xml gridview material-design

我有一个GridView,我想在其中显示应用了一些高程的网格项。我尝试了来自此线程here的解决方案,但在运行时未显示高程。高程显示在预览构建器中。

我的代码是

  1.   

    fragment_grid.xml

    <RelativeLayout
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:background="@drawable/grid_border"
        android:elevation="15dp"
        android:gravity="center"
        android:padding="4dp">
    
        <ImageView
            android:id="@+id/backgroundImg"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentTop="true"
            android:background="@drawable/image_border"
            android:elevation="1dp"
            android:src="@drawable/icon" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:padding="2dp">
    
            <LinearLayout
                android:layout_width="130dp"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:orientation="vertical"
                android:padding="1dp">
    
                <TextView
                    android:id="@+id/nameTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:text="Name"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/descriptionTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:text="Description" />
    
            </LinearLayout>
    
            <ImageButton
                android:id="@+id/moreBtn"
                style="?android:attr/actionOverflowButtonStyle"
                android:layout_width="20dp"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="4dp"
                android:foregroundGravity="center" />
    
        </RelativeLayout>
    </RelativeLayout>
    

  2.   

    fragment_gird_activity.xml |   此代码位于RelativeLayout,android:gravity为中心

    <GridView
        android:id="@+id/fragmentGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="150dp"
        android:gravity="center"
        android:horizontalSpacing="5dp"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="10dp" />
    

  3.   

    GridAvtivity.java,它扩展了AppCompatActivity

    private GridView fragmentGridView;
    private DetailsBox adapter;
    private final ArrayList<Details> details = new ArrayList<>();
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.album_activity);
    
        fragmentGridView = (GridView) findViewById(R.id.fragment_gird_activity);
        fillupData();
        adapter = new DetailsBox(getApplicationContext());
        fragmentGridView.setAdapter(adapter);
    }
    
    class DetailsBox extends ArrayAdapter<Details> {
    
        private Context ctx;
    
        public DetailsBox(@NonNull Context context) {
            super(context, R.layout.fragment_grid, details);
            this.ctx = context;
        }
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View newView = inflater.inflate(R.layout.fragment_grid, parent, false);
            Details detail = details.get(position);
            ((TextView) newView.findViewById(R.id.nameTV)).setText(detail.getName());
            ((TextView) newView.findViewById(R.id.descriptionTV)).setText(detail.getDescription());
            return newView;
        }
    }
    
    
    private void fillupData() {
        details.add(new Details("Name 1", "Description 1"));
        details.add(new Details("Name 2", "Description 2"));
        details.add(new Details("Name 3", "Description 3"));
        details.add(new Details("Name 4", "Description 4"));
        details.add(new Details("Name 5", "Description 5"));
        details.add(new Details("Name 6", "Description 6"));
        details.add(new Details("Name 7", "Description 7"));
        details.add(new Details("Name 8", "Description 8"));
        details.add(new Details("Name 9", "Description 9"));
        details.add(new Details("Name 10", "Description 10"));
        details.add(new Details("Name 11", "Description 11"));
        details.add(new Details("Name 12", "Description 12"));
        details.add(new Details("Name 13", "Description 13"));
        details.add(new Details("Name 14", "Description 14"));
        details.add(new Details("Name 15", "Description 15"));
        details.add(new Details("Name 16", "Description 16"));
        details.add(new Details("Name 17", "Description 17"));
        details.add(new Details("Name 18", "Description 18"));
        details.add(new Details("Name 19", "Description 19"));
        details.add(new Details("Name 20", "Description 20"));
    }
    
  4.   

    image_border.xml

  5. image_border.xml

    1.   

      grid_border.xml

    2. grid_border.xml

      以下在预览窗口中生成此结果 Result in Preview Window

      并且在运行时,这会产生以下结果

      enter image description here

      请帮助,我尝试了其他线程的解决方案,但无法在运行时添加高程。

1 个答案:

答案 0 :(得分:1)

简单的解决方案 - 将行布局包装到cardView

<android.support.v7.widget.CardView
    android:id="@+id/vCard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/_6sdp"
    android:clipChildren="false"
    app:cardBackgroundColor="@android:color/white"
    app:cardElevation="6dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="false">

    <!-- Your row layout codes -->

 </android.support.v7.widget.CardView>