【问题标题】:Android layout margins making width be 0Android布局边距使宽度为0
【发布时间】:2017-09-10 05:04:21
【问题描述】:

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

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

布局中的我的按钮:

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

【问题讨论】:

  • 你能分享你的布局文件吗?
  • 我为按钮添加了布局文件

标签: java android android-layout android-relativelayout


【解决方案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 放在最后。如果它不起作用,请见谅。

【讨论】:

  • 我已经有 dp 在它的末尾,即使我没有自动添加 dp tp 它。
【解决方案2】:

删除这个:

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

【讨论】:

  • 朋友,你的问题。
猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 2012-07-28
  • 2012-07-11
相关资源
最近更新 更多