【问题标题】:different values in onProgressChanged() and onStopTrackingTouch in SeekBarSeekBar 中的 onProgressChanged() 和 onStopTrackingTouch 中的不同值
【发布时间】:2012-09-27 21:07:49
【问题描述】:

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

现在我想检索按钮的高度。所以我在SeekBaronProgressChanged()方法中使用了btn1.getHeight()
但它为按钮高度提供了一组不正确/旧的值。

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

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

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

还有其他方法可以做到这一点吗?任何帮助表示赞赏。

编辑

@Luksprog:我做了以下更改:

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

活动中

RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(20,
            2 * progress * 1);
    lp2.setMargins(55, 0, 0, 0);
    btnGraph01.setLayoutParams(lp2);

RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
            new ViewGroup.MarginLayoutParams(20,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
    lp3.setMargins(55, 0, 0, 30);
    tvGraph01.setLayoutParams(lp3);
    tvGraph01.setTextSize(6);
    tvGraph01.setGravity(Gravity.CENTER);

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

如果我错了,请纠正我...

【问题讨论】:

  • 我的猜测是你在onProgressChanged方法中修改了ButtonLayoutParams。如果是这种情况,是什么阻止您直接使用新高度的计算值?
  • 我没有设置按钮的确切高度。我正在使用 weight 属性,因此高度为 0dp。

标签: android android-layout seekbar android-button


【解决方案1】:

首先我猜这与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>

现在在代码中不需要修改tvGraph01LayoutParams,它会随着Button 的增长/收缩而跟随LayoutParamsButton 并改为修改它一次又一次地创造新的:

RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams)btnGraph01.getLayoutParams();
lp2.width = 20;
lp2.height = 2 * progress * 1;
lp2.setMargins(55, 0, 0, 0); // <= do you really need the margin? what is its purpose?
btnGraph01.setLayoutParams(lp2);
tvGraph01.setTextSize(6);

【讨论】:

  • 我尝试了相对布局,但随后 textview 显示在按钮内。我不知道为什么会这样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-13
相关资源
最近更新 更多