Modifying button height dynamically in upward direction

瘦欲@ 提交于 2019-12-12 00:50:31

问题


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.

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"/>

Activity

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);
}

Am I doing anything silly? Any help appreciated.


回答1:


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));

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



来源:https://stackoverflow.com/questions/12562028/modifying-button-height-dynamically-in-upward-direction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!