【问题标题】:Android: setting leftMargin property to a button automatically resizes itAndroid:将 leftMargin 属性设置为按钮会自动调整其大小
【发布时间】:2014-09-16 23:44:33
【问题描述】:

我的布局中有一个简单的按钮。将leftMargin 设置为实际显示不同结果的视图。

my_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/left_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="hello pandora"/>

</RelativeLayout>

在我的活动中,我将leftMargin 属性设置为按钮。

Button leftBtn = (Button) findViewById(R.id.left_btn);
LayoutParams params = (LayoutParams) leftBtn.getLayoutParams();
params.leftMargin = 550;

如果我将leftMargin 设置为负值或0,它工作正常,但如果我设置的值大于屏幕宽度,它只是调整大小/压缩按钮。我预计按钮会像负值一样超出范围。

我预计第三张图片中的按钮会像第一张图片中的按钮一样越界。

请不要说在布局中设置按钮layout_alignParentRight="true",在活动中设置rightMargin = -50(这可行),因为我想将按钮从左向右移动。

【问题讨论】:

  • 如果您获得按钮的宽度并以 dps 中的实际宽度以编程方式覆盖 wrap_content 会发生什么?另一种猜测是将父视图 paddingRight 设置为负值。
  • @Gyebro 我已经尝试了你的第一个解决方案,但它实际上是删除了整个文本“pandora”。请查看我对 raj 答案的评论,因为它显示了相同的结果。对于您的第二个解决方案,将 paddingRight 设置为负值时位置根本不会改变。

标签: android margin


【解决方案1】:

我假设为父级 RelativeLayout 分配一个大于屏幕尺寸的特定宽度(例如 1000 dp)应该可以解决您的问题。

还有,为什么要制作屏幕外的 UI 元素?期望的行为是什么?也许过渡动画会更好?

编辑

我已经尝试过动画 + 存储按钮的测量宽度。它似乎工作。 你可以在 GB 上试试这个吗?

MainActivity.java

public class MainActivity extends Activity {

final Context context = this;
Button mButton;
int mButtonWidth; // Measured width of Button
int amountToMove; // Amount to move the button in the x direction

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    amountToMove = 600;

    mButton = (Button) findViewById(R.id.button);
    // Measure Button's width
    mButton.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    mButtonWidth = mButton.getMeasuredWidth();
    // Simple onClick listener showing a Toast
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context,"Hello Pandora clicked!",Toast.LENGTH_SHORT).show();
        }
    });

    // Onclick listener for the other button
    Button toggle = (Button) findViewById(R.id.toggle);
    toggle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Animate the other button
            TranslateAnimation a = new TranslateAnimation(0, amountToMove, 0, 0);
            a.setDuration(1000);
            // Finalize movement when animation ends
            a.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mButton.getLayoutParams();
                    // Restore measured width and change left margin
                    lp.width = mButtonWidth;
                    lp.leftMargin = lp.leftMargin + amountToMove;
                    mButton.setLayoutParams(lp);
                    amountToMove = -amountToMove;
                }
                @Override
                public void onAnimationStart(Animation animation) { /* Do nothing */ }
                @Override
                public void onAnimationRepeat(Animation animation) { /* Do nothing */ }
            });
            mButton.startAnimation(a);
        }
    });
}
}

activity_main.xml

<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello Pandora"
    android:id="@+id/button" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Move the other button"
    android:id="@+id/toggle"/>

</LinearLayout>

编辑 2

它也适用于 GB 模拟器(按钮被剪裁,可点击)。

【讨论】:

  • 是的,翻译动画效果很好。我只想将按钮从左到右移出屏幕。但我不知道为什么 marginLeft 不起作用。
  • 您好,很抱歉翻译动画出现问题,因为触摸事件在 GingerBread (2.3v) 中的动画路径上一直被触发。
  • @DileepPerla 我已经编辑了我的答案。你能在 GingerBread 上试试这个吗?
  • 您好,很抱歉我的回答迟了。实际上它根本不起作用。我已经尝试过类似的方法。问题出在onAnimationEnd,这里当我们将动画从 0 转换为 amountToMove 时,在动画结束时,leftMargin 仍然保持为 0,但按钮​​已移动,当我们在 onAnimationEnd 中增加 leftMargin 时,它进一步移动,超出 amountToMove。我只在 GingerBread 中遇到了所有这些问题,而且 ICS+ 版本工作正常。
  • 我的最终理解是,当我们使用TranslateAnimation 移动时,按钮在视觉上移动但对象保持在原始位置,触摸事件也是如此。当我们使用 leftMargin 移动时,对象也会移动,并且触摸事件可以正常工作,但按钮大小会压缩,如问题所示。这只是gingerBread 中的问题。
【解决方案2】:

当您使用 leftMargin = 550 时,您可以使用 max line=1 在按钮上的一行中显示完整的文本;

试试这个

 <Button
    android:id="@+id/left_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:maxLines="1"
    android:text="hello pandora"/>

【讨论】:

  • 它工作的部分原因是maxLines=1 只是删除了多余的行,所以文本“pandora”被完全删除,我期待像“hello pand”这样的东西。它目前只显示“你好”。
【解决方案3】:

你好,像这样编辑你的按钮属性,

android:layout_gravity="center_horizo​​ntal"

android:singleLine="true"

并将父布局更改为 frameLayout

【讨论】:

  • 不,它只是在按钮中省略了我的文本,在按钮文本的末尾得到点(..)。
猜你喜欢
  • 1970-01-01
  • 2019-11-20
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多