Android - 如何在 GridLayout 中为自定义 LinearLayouts 设置边距?

标签 android android-linearlayout margin android-gridlayout layoutparams

我在为我在 GridLayout 中多次使用的自定义线性布局类设置边距时遇到问题。 Gridlayout 放置在 fragment 中。 这是 fragment_grid.xml 的代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app_a_tize.expressme.Fragment.GridFragment"
android:layout_gravity="center">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/orange"
    android:layout_margin="5dp"
    android:id="@+id/gridlayout_grid"></GridLayout>
</FrameLayout>

这是 GridFragment.java 的代码:

public class GridFragment extends Fragment {
public GridFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_grid, container, false);
}

@Override
public void onStart() { 
    super.onStart();
    GridLayout grid = (GridLayout) getView().findViewById(R.id.gridlayout_grid);
    grid.setRowCount(3);

    int tileHeight = (CategoryTileActivity.gridContentHeight -3 * 10) / 3;
    int amountofColumns = (int) CategoryTileActivity.gridContentWidth / tileHeight;
    grid.setColumnCount(amountofColumns);
    grid.setMinimumWidth((amountofColumns * tileHeight) + (5 * 20 ));

    for (int i = 0; i < 3 * amountofColumns; i++) {
    //fill the grid with the custom LinearLayout:
        grid.addView(new TileClass(getActivity(), tileHeight, tileHeight, "ToBeImplemented", "Button"));
    }

}
}

这是自定义LinearLayout的代码:

public class TileClass extends LinearLayout {
public TileClass(Context context, int height, int width, String image, String text) {
    super(context);
    this.setBackgroundResource(R.drawable.tile_button); //creates rounded layouts
    this.setMinimumHeight(height);
    this.setMinimumWidth(width);
    this.setOrientation(LinearLayout.VERTICAL);

    ImageView tileImage = new ImageView(context);

    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.tilephoto);
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 100, 100, true);
    tileImage.setImageBitmap(bMapScaled);

    tileImage.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    TextView tileText = new TextView(context);
    tileText.setText(text);
    tileText.setTextColor(Color.WHITE);
    tileText.setGravity(Gravity.CENTER);

    addView(tileImage);
    addView(tileText);
}
}

当我运行该 Activity 时,我得到以下结果: Here you can see there is no margin on every button in the orange area.

我上面显示的代码负责中间的橙色区域。

我需要的是:中间橙色区域的蓝色“按钮”/LinearLayouts,边距为 5dp。所以其余的橙色空间由自定义 LinearLayouts 占用。

我不知道如何解决这个问题,我尝试了很多选项,但它们似乎都不适合我。从 MarginLayoutParams 到 params.setMargins(5,5,5,5); 的一切;在我的代码中的几乎所有布局上。

我使用的是 Android Studio 2.1.2,最低支持 API 15。

感谢您的帮助!
对于你的想象,这一定是最终结果,我需要这样的 margin : This is the endresult of the GridLayout in the middle

最佳答案

您必须如下所示制作 gridview 项目的自定义 View :-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/categoryHeight"
    android:layout_marginLeft="@dimen/margin_5dp"
    android:layout_marginRight="@dimen/margin_5dp"
    android:layout_marginTop="@dimen/margin_7dp"
    android:background="@drawable/rounded_bg"
>

    <ImageView
        android:id="@+id/llRowItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scaleType="fitXY"
        android:gravity="bottom"/>

        <TextView
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/black_light"
            android:padding="@dimen/margin_5dp"
            android:layout_gravity="bottom"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textSize="@dimen/font_size_16sp" />
</FrameLayout>

和内部适配器设置 TextView 的颜色、背景、文本或 ImageView 的图像,无论您想要设置什么。

关于Android - 如何在 GridLayout 中为自定义 LinearLayouts 设置边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657786/

相关文章:

html - 详细信息标签文本不响应 css 边距

android - 将 Android fragment 移动到不同的容器无法更改 fragment 的容器 ID

android - Gradle - 将依赖项添加到另一个模块的测试

java - Android FireBase 存储下载 - 如何检测网络中断?

android - 如何防止 TextView 拉伸(stretch)其父 LinearLayout?

flutter - 如何将 Flutter Chip 小部件的顶部和底部边距设置为零?

android - 如何使用 WebView 在新窗口中启动 Activity ?

xml - android View 中经常出现的问题,解析XML时出错: unbound prefix

java - LinearLayout 无法转换为我创建的类

html - 边距防止文本对齐;