动态修改按钮高度在向上的方向

动态修改按钮高度在向上的方向

本文介绍了动态修改按钮高度在向上的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用图形按钮状结构。结果我想修改这些按钮的高度动态的基于搜索栏的值。

I'm trying to create graph-like structure using buttons.
I want to modify the height of these buttons dynamically based on the value of seek bar.

我能够实现这个如本屏幕截图1.但问题是按钮生长在向下方向的高度(这是他们的默认行为)。

I'm able to implement this as shown in this screen shot 1. But the problem is buttons grows in height in downward direction (which is their default behavior).

我如何作出如下图所示按钮向上生长?

How do I make buttons grow upward as shown below?

XML
    

    <Button
        android:id="@+id/btnGraph"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/seekBar"
        android:background="#99f"/>

     <Button
        android:id="@+id/btnGraph2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/seekBar"
        android:layout_toRightOf="@id/btnGraph"
        android:background="#9f9"/>


活动

public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromTouch) {
    tvValue.setText("Value = " + progress);

 // NEGATIVE HEIGHT WONT WORK HERE...
    lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
            50, progress * 10));
    lp.setMargins(10, 300, 0, 0);
    btnGraph.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
            50, progress * 5));

    lp.setMargins(100, 300, 0, 0);
    btnGraph2.setLayoutParams(lp);
}

我做做傻事?任何帮助AP preciated。

Am I doing anything silly? Any help appreciated.

推荐答案

可以在的LinearLayout,因为没有涉及的相对位置来实现更容易一点。翘曲的按钮的相对布局和使用权重属性控制按钮的高度。

It can be accomplished a bit easier in LinearLayout since there is no relative positions involved. Warp the buttons in a relative layout and control the height of the button using the weight attribute.

XML

<RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentLeft="true"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"
android:weightSum="1"
android:orientation="vertical"
android:gravity="bottom">

<Button
    android:id="@+id/sample_button"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:text="Hello"
   />
</LinearLayout>
</RelativeLayout>

和可以编程设定的权重如下:

and you can programatically set the weight as follows

b.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 0,count*0.1f));

这对我的作品。最重要的事情在这里却是设置的LinearLayout的比重(你也可以做到这一点编程)。

this works for me. The important thing here however is setting the gravity of the LinearLayout (you can also do it programatically).

这篇关于动态修改按钮高度在向上的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:20