Android-如何在GridLayout中设置自定义线性布局的边距?
我在设置GridLayout中多次使用的自定义线性布局类的边距时遇到问题。Gridlayout被放置在片段中。 这是fragment_grid.xml的代码:Android-如何在GridLayout中设置自定义线性布局的边距?,android,android-linearlayout,margin,android-gridlayout,layoutparams,Android,Android Linearlayout,Margin,Android Gridlayout,Layoutparams,我在设置GridLayout中多次使用的自定义线性布局类的边距时遇到问题。Gridlayout被放置在片段中。 这是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:layo
<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>
当我运行“活动”时,我得到以下结果:
<>我上面显示的代码负责中间的橙色区域。BR/> BR/>我所需要的:蓝色的“按钮”/线性布局,在橙色区域中间,有5DP的余量。因此,橙色空间的其余部分由自定义线性布局占据
我不知道如何解决这个问题,我尝试了很多选择,但它们似乎不适合我。。从MarginLayoutParams到params.setMargins(5,5,5,5)的所有内容;在我的代码中的几乎每个布局上
我使用Android Studio 2.1.2,支持API 15的最低要求
感谢您的帮助对于你的想象,这一定是最终结果,我需要这样的空白:
您必须按如下方式创建gridview项目的自定义视图:-
<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>
和内部适配器设置文本视图的颜色、背景、图像视图的文本或图像,无论您想设置什么。您需要获取LinearLayout.LayoutParams的对象并设置边距,所有这些都在您的TileClass中,这将帮助您完成任务,但您的解决方案不起作用。谢谢,您的评论解决了我的问题。现在我需要编写适配器,我似乎并不真正理解。。但是谢谢你的帮助!
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);
}
}
<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>