gpt4 book ai didi

android - 旋转的垂直按钮不与左父对齐

转载 作者:行者123 更新时间:2023-11-29 17:59:01 33 4
gpt4 key购买 nike

我有一个旋转 -90 度的按钮。旋转按钮未与父按钮左对齐。

XML如下

<RelativeLayout
android:id="@+id/rl_showChapters"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#000000"
android:visibility="gone" >

<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:rotation="-90"
android:text=" Show Chapters " />

</RelativeLayout>

以下是输出。 enter image description here

但是我想把这个按钮留在左边。

最佳答案

我知道这是一个老问题,但我想在这里留下一个答案以供将来引用。

除了我想将我的按钮与其父级的右侧对齐外,我遇到了完全相同的问题。这里的问题是,在旋转 View 时,将选择枢轴作为其中心,除非另有说明(我不知道该怎么做)。我找不到任何属性来为我完成这项工作;所以我实现了以下方法:

private void transformShowPopupButton() {
int w = btn_showPopup.getWidth(), h = btn_showPopup.getHeight();
int diff = w/2-h/2;
btn_showPopup.setRotation(270);
btn_showPopup.setTranslationX(diff);
}

基本上,此方法会移动按钮,直到它与其父按钮对齐。移动量等于 getWidth()/2getHeight()/2 之间的差值(当然我没有任何边距,我让按钮对齐与其父项。如果您指定了边距值,则还需要将其相加。)

要将按钮与其父按钮的左侧对齐,您需要通过-diff 对其进行翻译。

重要:为了能够获得 View 的高度和宽度,btn_showPopup 在我的例子中,调用 transformShowPopupButton()onCreate() 将不起作用。所以在 onWindowFocusChanged() 中调用它就可以了。

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
transformShowPopupButton();
}

关于android - 旋转的垂直按钮不与左父对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17313799/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com