gpt4 book ai didi

java - Material 设计的 GridView 中未显示标高

转载 作者:行者123 更新时间:2023-12-02 02:24:43 30 4
gpt4 key购买 nike

我有一个 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 | This code is in RelativeLayout with android:gravity as center

    <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, it extends 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

image_border.xml

  • grid_border.xml

  • grid_border.xml

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

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

    enter image description here

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

    最佳答案

    简单的解决方案 - 将行布局包装到 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>

    关于java - Material 设计的 GridView 中未显示标高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47969525/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com