Android通过左右拖动搜索栏来控制视图高度
我的问题是我有一个视图和seekbar。现在我想借助seekbar来控制视图的高度。也就是说,当seekbar向右拖动时,视图高度会增加,而向左拖动时,视图高度会减少…我想要它的是什么,我在示例图像中进行了解释Android通过左右拖动搜索栏来控制视图高度,android,Android,我的问题是我有一个视图和seekbar。现在我想借助seekbar来控制视图的高度。也就是说,当seekbar向右拖动时,视图高度会增加,而向左拖动时,视图高度会减少…我想要它的是什么,我在示例图像中进行了解释 当我向右拖动seekbar时,视图高度会增加。当我向左拖动seekbar时,视图高度会降低。任何帮助都将不胜感激。提前感谢。这是一个粗略的代码,但希望您能从中了解 SeekBar SeekBar=(SeekBar)layout.findViewById(R.id.SeekBar); se
当我向右拖动seekbar时,视图高度会增加。当我向左拖动seekbar时,视图高度会降低。任何帮助都将不胜感激。提前感谢。这是一个粗略的代码,但希望您能从中了解 SeekBar SeekBar=(SeekBar)layout.findViewById(R.id.SeekBar); seekBar.setProgress(1);seekBar.incrementProgressBy(10); seekBar.setMax(100) seekBar.setonseekbarchaneglistener(新 SeekBar.onseekbarchaneglistener(){ 框架布局。布局参数(30,高度); }
我已经实现了演示。根据您的要求调整高度。希望您能有所了解 main活动
@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));
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar_布局);
((SeekBar)findViewById(R.id.sbHeight)).setOnSeekBarChangeListener(新的OnSeekBarChangeListener(){
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){
}
@凌驾
更改了公共进程无效(SeekBar SeekBar,int progress,
布尔值(用户){
如果(进度>0&&进度<30)
进展=40;
否则如果(进度>30和进度<60)
进展=60;
否则,如果(进度>60&&进度<100)
进展=80;
如果(进度!=0)
设置视图高度(进度);
}
});
((LinearLayout)findViewById(R.id.llHeight)).setLayoutParams(新的LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,60));
}
私有void setViewHeight(内部进度)
{
((LinearLayout)findViewById(R.id.llHeight)).setLayoutParams(新的LinearLayout.LayoutParams(LayoutParams.FILL_父项,进度));
}
seekbar\u 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>
public void onStopTrackingTouch(SeekBar-SeekBar){if(progress==0){progress=10;}什么是进度?对不起,我弄错了。我已经编辑了代码。现在检查它…代码必须在onProgressChanged中。这里有一个用于进度的参数。非常抱歉,伙计,我已经解决了我的问题。非常感谢。。非常感谢:)
@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));
}
<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>