SeekBar中onProgressChanged()和onStopTrackingTouch中的不同值

时间:2012-09-26 12:00:43

标签: android android-layout seekbar android-button

我正在根据搜索栏进度动态修改按钮的高度。

现在我想要检索按钮的高度。所以我在btn1.getHeight()的{​​{1}}方法中使用了onProgressChanged() 但它为按钮高度提供了不正确/旧的值集。

相反,如果我在SeekBar中使用btn1.getHeight(),我会得到正确的值。

这只是意味着,当我尝试在onStopTrackingTouch()中检索按钮的高度时,屏幕上会显示UI更改但尚未注册会导致获取不正确/旧值。< / p>

如何在onProgressChanged()中获取正确的值?

还有其他方法吗?任何帮助表示赞赏。

修改

@Luksprog:我做了以下更改:

onProgressChanged()

和活动

<SeekBar
    android:id="@+id/seekBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:maxHeight="9dp"
    android:minHeight="9dp"
    android:padding="10dp"
    android:max="100"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/shine_btn" />

<RelativeLayout
    android:id="@+id/Graph01"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_below="@id/seekBar"
    android:layout_marginTop="50dp"
    android:gravity="bottom" >

    <Button
        android:id="@+id/btnGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                        
        android:layout_alignParentBottom="true"
        android:background="#99f" />

    <com.example.calci.views.MyTextView
        android:id="@+id/tvGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnGraph01" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/Graph02"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_below="@id/seekBar"
android:layout_toRightOf="@id/Graph01"
    android:layout_marginTop="50dp"
    android:gravity="bottom" >

    <Button
        android:id="@+id/btnGraph02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                        
        android:layout_alignParentBottom="true"
        android:background="#99f" />

    <com.example.calci.views.MyTextView
        android:id="@+id/tvGraph02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnGraph02" />
</RelativeLayout>

</RelativeLayout>

但是TextView会显示在按钮内......为什么会这样?

如果我“错误......

,请纠正我

1 个答案:

答案 0 :(得分:2)

首先我猜这与your previous question有关。我不会使用weights,而只是使用RelativeLayout并将Buttons添加到底部,这样Buttons的高度会增加。像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent" >

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:max="200"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#99cc00"
        android:text="Button"
        android:onClick="test" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/button1"
        android:background="#0077cc"
        android:text="Button" />

</RelativeLayout>

其次,我不明白为什么你不能达到理想的高度。使用button.getHeight()上面的代码,您将获得之前的高度存储(因为在您离开onProgressChanged之前不会存储新的高度)。但这不应该阻止你获得新的高度,因为你实际上是通过以下方式计算这个值:

progress * 10

代码:

@Override

    public void onProgressChanged(SeekBar seekBar, final int progress,
                        boolean fromUser) {
                    LayoutParams lp = findViewById(R.id.button1).getLayoutParams();
                    lp.width = 50;
                    lp.height = progress * 10;
                    findViewById(R.id.button1).setLayoutParams(lp);
                    Log.e("XZX", "Progress : " + progress + " |Old height : "
                            + findViewById(R.id.button1).getHeight()
                            + "  |New height : " + (progress * 10));
                }

编辑: 您的布局文件错误:

<RelativeLayout
    android:id="@+id/Graph01"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_below="@id/seekBar"
    android:layout_marginTop="50dp" >

    <Button
        android:id="@+id/btnGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                        
        android:layout_alignParentBottom="true"
        android:background="#99f" />

    <com.example.calci.views.MyTextView
        android:id="@+id/tvGraph01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnGraph01" />
</RelativeLayout>

现在在代码中无需修改LayoutParams的{​​{1}},它会随着tvGraph01的增长/收缩而关注,也会获得当前的Button LayoutParams并修改它而不是一次又一次地创建新的:

Button