【问题标题】:Button's height automatically changes android按钮的高度自动改变android
【发布时间】:2013-07-01 13:01:54
【问题描述】:

我有一个带有水平排列按钮的 LinearLayout、一个带有水平方向 TextViews 的 LinearLayout、一个 gridview 和一个 TextView 的 Activity。 第一个 LinearLayout 有 3 个按钮,其中一个最初的可见性设置为“GONE”。 LinearLayout 的权重必须是最初的 2/28,但是当第三个按钮出现时,它会完全重塑。我还提到,当我在更大的屏幕上进行测试时,比例保持不变,并且一切正常。使用的drawable是一个带有圆角和描边的简单形状。

    <!-- this button is initially set as GONE and then it changes his height -->
            <Button 
                android:id="@+id/previous"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="end"
                android:gravity="center"
                android:background="@drawable/button"
                android:layout_marginRight="10dp"
                android:text="@string/previous"
                android:textColor="#606060"
                android:textStyle="bold"
                android:textSize="20sp"
                android:hint="Previous"/>

<!-- This button is visible from the very beginning, with the correct height -->
            <Button 
                android:id="@+id/next"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="end"
                android:gravity="center"
                android:background="@drawable/button"
                android:text="@string/next"
                android:textColor="#606060"
                android:textStyle="bold"
                android:textSize="20sp"
                android:hint="Next"/>
            </LinearLayout>

我提到我以编程方式设置了可见性,它具有任何相关性:

_previous.setVisibility(GONE);

分别

_previous.setVisibility(VISIBLE);

知道为什么会发生这种情况,我该如何解决?

这里有两张照片可以更好地描述我的想法: Img1 Img2 谢谢!

【问题讨论】:

    标签: android button layout height


    【解决方案1】:

    我不明白你的意思

    _previous.setVisibility(GONE); and respectively _previous.setVisibility(VISIBLE);

    我认为你的意思是用View.GONE 隐藏按钮并用View.VISIBLE 显示它。

    您了解View.GONEView.INVISIBLE 之间的区别吗?

    View.GONE 使布局自行测量,就好像按钮从未存在过一样。 View.INVISIBLE 导致布局测量自身,包括按钮,但不在屏幕上显示。如果一开始设置为View.GONE,然后设置为View.VISIBLE,整个布局会重新测量重绘。

    在不理解您的问题的情况下,我猜您应该在代码中使用View.INVISIBLEView.VISIBLE

    您还应该在 XML 中使用 android:visibility="invisible" 作为初始状态。

    【讨论】:

    • 是的,我知道消失、不可见和可见之间的区别。我需要按钮消失,因为当屏幕上不存在按钮时,我需要将 textview 居中,如果按钮设置为不可见,我无法做到这一点。更明确地说,我希望该按钮仅在未显示当前月份时将我引导至上个月。我的问题是为什么按钮不像其他按钮一样,尽管我使用相同的规格(它也会改变其他按钮的高度)。
    【解决方案2】:

    我设法通过获取 LayoutParams 并将高度设置为屏幕的 2/28 来解决问题,因为它应该从一开始就应该是这样,现在它就像魅力一样工作。 以下是源代码:

    DisplayMetrics _metrics = getResources().getDisplayMetrics();
    int _height = _metrics.heightPixels; //height of the screen
    
    LayoutParams _layoutParamsButton = (LayoutParams) _previous.getLayoutParams(); 
    _layoutParamsButton.height = _height/28*2;
    _previous.setLayoutParams(_layoutParamsButton);     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多