布局项位置在第一行移动,第二行正常。问题出现在不同的布局上

问题描述 投票:0回答:0

试图在

GridLayout
中获取我的物品,但问题似乎也出现在其他布局上。

这是它的样子:

当我通过

setGravity(Gravity.BOTTOM)
选项更改文本位置时,问题首先出现。它将图像的绝对位置移动 1 个项目大小。我用
setTranslationY(120)
对抗它并且它适用于第一行,但第二行项目向下移动了 120px。从理论上讲,我可以用 TranslationY 捕获前 3 个项目,只是不将其应用于较低的位置,但这非常蹩脚。

有什么办法可以同时解决这个问题吗?

按钮生成代码:

Button myButton = new Button(this);
            myButton.setLayoutParams(new ViewGroup.LayoutParams(360, 360));
            myLayout.addView(myButton);
            String str = String.format("Button_%s", i);
            String[] mainStr=storedItems2[i].split("<<>>");
            String imgPath = mainStr[1].replaceFirst(">><<","");
            String description = mainStr[0];
            myButton.setText(description);
            myButton.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            int imageId=0;
            if (mainStr.length>2){
                imageId= Integer.parseInt(mainStr[2].replaceFirst(">><<",""));
            }
            else{
                imageId=R.drawable.orb;
            }

            myButton.setBackgroundResource(imageId);
            myButton.setAllCaps(false);
            myButton.setTextColor(Color.parseColor("#333f49"));
            myButton.setGravity(Gravity.BOTTOM);
            myButton.setPadding(25,0,0,25);

布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"></LinearLayout>

    <ScrollView
        android:id="@+id/to_Resize"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <LinearLayout
            android:id="@+id/text_block"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="true"
            android:orientation="vertical" />
    </ScrollView>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF">

        <GridLayout
            android:id="@+id/pew1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="4"
            android:rowCount="12" />
    </ScrollView>

</LinearLayout>
java android button layout grid-layout
© www.soinside.com 2019 - 2024. All rights reserved.