Android Seek条形文字宽度超出范围

时间:2016-02-12 05:43:37

标签: android

enter image description here我在seek bar使用text view。当我更改seek bar时,text view也会移动。当我的seek bar text view的最后一个位置超出范围时。

拜托,在我做错的任何一个帮助。

我在这里分享我的code。当我更改seek bar时,我想移动text view以及搜索栏位置。

    // Seek bar code         
    seekbar_pay_restaurant_.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    //change position of seekebar tool tip according to seekbar progress               
                    int x = seekBar.getThumb().getBounds().left;
                    Log.v("y value ",""+x);
                    int width = seekBar.getThumb().getMinimumWidth();
                    int textWidth=tv_tool_type_food_.getMeasuredWidth();
                    Log.v("textview Width","width"+textWidth);
                    Log.v("width",""+ width);
                    tv_tool_type_food_.setX(x);
                    if (seekbar_pay_restaurant_.getProgress() == 0) {
                        tv_tool_type_food_.setText(getResources().getString(R.string.hint_fast_food__));
                        // tv_tool_type_food_.setX(y);
                        return ;
                    } else if (progress == 1) {
                        tv_tool_type_food_.setX(x);
                        tv_tool_type_food_.setText(getResources().getString(R.string.hint_fast_casual__));
                        return ;
                    } else if (progress == 2) {
                      // tv_tool_type_food_.setX(x );
                        tv_tool_type_food_.setText(getResources().getString(R.string.hint_casual__));
                        return;
                    } else if (progress == 3) {
                          tv_tool_type_food_.setText(getResources().getString(R.string.hint_fine_dyning__));
                        return;
                    }
                    tv_tool_type_food_.setText(getResources().getString(R.string.hint_fast_food__));
                }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                }
            });
// seek bar xml file 
<SeekBar
                                android:id="@+id/seekbar_pay_restaurant"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:max="3"
                                android:progress="0"
                                android:thumbOffset="@dimen/two_dp"
                                android:layout_marginRight="@dimen/twenty_dp"
                                android:layout_marginLeft="@dimen/five_dp"
                                />
<TextView android:id="@+id/tv_tool_type_food"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                style="@style/label_white_h13"
                                android:background="@drawable/tooltip_bg"
                                android:typeface="normal"
                                android:text="@string/hint_fast_food__"
                                android:gravity="center"
                                android:paddingLeft="@dimen/seven_dp"
                                android:paddingRight="@dimen/seven_dp"
                                android:paddingTop="@dimen/ten_dp"
                                android:paddingBottom="@dimen/ten_dp"
                                android:layout_marginRight="@dimen/one_sp"
                                android:layout_marginLeft="@dimen/one_dp"/>

2 个答案:

答案 0 :(得分:0)

// Seek bar code          
    seekbar_pay_restaurant_.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
                @Override 
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    //change position of seekebar tool tip according to seekbar progress                
                    int x = seekBar.getThumb().getBounds().left;
                    Log.v("y value ",""+x);
                    int width = seekBar.getThumb().getMinimumWidth();
                    int textWidth=tv_tool_type_food_.getMeasuredWidth();
                    Log.v("textview Width","width"+textWidth);
                    Log.v("width",""+ width);
                    if(x < max_x ) { // This will prevent the movement of text view out side
                       tv_tool_type_food_.setX(x);
                    }
                    if (seekbar_pay_restaurant_.getProgress() == 0) { 
                        tv_tool_type_food_.setText(getResources().getString(R.string.hint_fast_food__));
                        // tv_tool_type_food_.setX(y); 
                        return ; 
                    } else if (progress == 1) {
                        tv_tool_type_food_.setX(x);
                        tv_tool_type_food_.setText(getResources().getString(R.string.hint_fast_casual__));
                        return ; 
                    } else if (progress == 2) {
                      // tv_tool_type_food_.setX(x ); 
                        tv_tool_type_food_.setText(getResources().getString(R.string.hint_casual__));
                        return; 
                    } else if (progress == 3) {
                          tv_tool_type_food_.setText(getResources().getString(R.string.hint_fine_dyning__));
                        return; 
                    } 
                    tv_tool_type_food_.setText(getResources().getString(R.string.hint_fast_food__));
                } 
                @Override 
                public void onStartTrackingTouch(SeekBar seekBar) {
                } 

                @Override 
                public void onStopTrackingTouch(SeekBar seekBar) {
                } 
            }); 

计算该值的最佳方法是

int seek_bar_width = seekbar.getMeasuredWidth();
int text_view_with = textview.getMeasuredWidth();
int max_x = seekbar.getX()+seek_bar_width-text_view_with;

希望这有帮助!

答案 1 :(得分:0)

如果要使用搜索栏位置移动文本视图,则需要添加以下内容:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

if (textview.getX() > width-textview.getLayoutParams().width) {
   textview.setX(width-textview.getLayoutParams().width);
}

这个“如果”会阻止这样做。