有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android布局边距使宽度为0

当我试图在Java中更改某些内容的左边距或右边距时,它不起作用。相反,它会将按钮缩小到宽度为0dp的位置。 这是我的密码:

RelativeLayout.LayoutParams TwoPos = (RelativeLayout.LayoutParams) ezONEaONE.getLayoutParams();
TwoPos.bottomMargin = 515;
TwoPos.leftMargin = 50;
ezONEaONE.setLayoutParams(TwoPos);

布局中的“我的”按钮:

<Button
    安卓:text="@string/ez1a1"
    安卓:id="@+id/ez1a1"
    安卓:background="@安卓:color/holo_orange_dark"
    安卓:textSize="24sp"
    tools:ignore="Deprecated,NewApi,RtlHardcoded,TextViewEdits"
    安卓:onClick="Ez1A1"
    安卓:minWidth="88dp"
    安卓:minHeight="46dp"
    安卓:autoText="false"
    安卓:layout_height="wrap_content"
    安卓:layout_width="wrap_content"
    安卓:layout_marginBottom="10dp"
    安卓:typeface="serif"
    安卓:layout_above="@+id/ez1N1" />

共 (2) 个答案

  1. # 1 楼答案

    Button ezOneaOne = (Button) findViewById(R.id.ezOneaOneIDName);
    RelativeLayout.LayoutParams TwoPos = (RelativeLayout.LayoutParams) ezONEaOne.getLayoutParams();
    TwoPos.setMargins(50, 0, 0, 515); //left, top, right, bottom
    ezOneaOne.setLayoutParams(TwoPos);
    

    您可能必须将dp放在末尾。对不起,如果它不起作用

  2. # 2 楼答案

    删除此项:

    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/ez1N1"
    android:layout_alignEnd="@+id/ez1N1"