Android通过拖动搜索栏右和左控制视图高度

时间:2013-12-09 12:43:31

标签: android

enter image description here

我的问题是我有一个View和seekbar.now我想在SeekBar的帮助下控制视图的高度。当SeekBar向右拖动时视图高度增加,向左拖动时视图高度减小。 ..我想要它,我已经在示例图片中解释..

当我向右拖动搜索栏时,视图高度会增加。当我向左拖动搜索栏时,视图高度会降低。任何帮助都会非常感激。谢谢Alot提前。

2 个答案:

答案 0 :(得分:2)

我已根据您的要求实现了demo.Adjust高度。希望您能有所了解。

<强> MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.seekbar_layout);

    ((SeekBar)findViewById(R.id.sbHeight)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {

            if(progress > 0 && progress < 30)
                progress = 40;
            else if(progress > 30 && progress < 60)
                progress = 60;
            else if(progress > 60 && progress < 100)
                progress = 80;

            if(progress != 0)
                setViewHeight(progress);                
        }
    });

    ((LinearLayout)findViewById(R.id.llHeight)).setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,60));        
}

private void setViewHeight(int progress)
{
    ((LinearLayout)findViewById(R.id.llHeight)).setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,progress));
}

<强> seekbar_layout.xml

 <RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" 
   >
    <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical" 
       android:gravity="center"
       android:layout_marginTop="20dp"
       >
        <LinearLayout 
           android:id="@+id/llHeight"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="vertical" 
           android:background="#3296ed"
           >
        </LinearLayout>
    </LinearLayout>

    <SeekBar 
        android:id="@+id/sbHeight"
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:progress="50"
    android:layout_marginBottom="140dp"
    android:layout_alignParentBottom="true"
    />
 </RelativeLayout>

答案 1 :(得分:0)

这是一个很好的代码,但希望你能从中得到它的想法

  

SeekBar seekBar =(SeekBar)layout.findViewById(R.id.seekbar);   seekBar.setProgress(1); seekBar.incrementProgressBy(10);   seekBar.setMax(100);

     

seekBar.setOnSeekBarChangeListener(新   SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
   if(progress == 0)          {
          progress = 10;          }
                  int height = progress/10; //let suppose 10 is your maximum height of your view to resize
      linearlayoutToResize.setLayoutParams(new
     

FrameLayout.LayoutParams(30,height));       }

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {            

} });