2012-09-24 3 views
0

버튼을 사용하여 그래프와 같은 구조를 만들려고합니다.
시크 바 (seek bar)의 값에 따라이 버튼의 높이를 동적으로 수정하고 싶습니다. 이 화면에서와 같이 내가 이것을 구현할 수있어위쪽 방향으로 버튼 높이를 동적으로 수정하십시오.

screenshot

1. 촬영 그러나 문제는 버튼 (자신의 기본 동작) 아래 방향으로 높이 성장입니다.


아래 그림과 같이 버튼을 위로 자라게하려면 어떻게합니까? screenshot

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

나는 바보 아무것도 건가요? 어떤 도움을 주셔서 감사합니다.

+0

버튼의 부모보기는 무엇입니까? 선형 레이아웃 인 경우 중력을 아래쪽으로 설정할 수 있습니다. – Gan

+0

상대 레이아웃 – GAMA

+0

'btnGraph.setGravity (Gravity.BOTTOM);'을 시도했지만 아무런 효과가 없습니다 ... – GAMA

답변

1

상대적 위치가 관련되어 있지 않으므로 LinearLayout에서 약간 더 쉽게 수행 할 수 있습니다. 상대적 레이아웃에서 버튼을 휘게하고 weight 속성을 사용하여 버튼의 높이를 제어합니다.

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> 

이 나를 위해 작동

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

을 다음과 같이 프로그래밍 방식으로 무게를 설정할 수 있습니다. 여기서 중요한 것은 LinearLayout의 중력을 설정하는 것입니다 (프로그래밍 방식으로도 할 수 있습니다).