我有一个搜索栏,我在其上放置了一个textview,显示了搜索栏移动的值。我的问题是Textview和Seekbar的对齐最初是相同的,但拖动对齐更改我附加了屏幕截图和代码。
XML文件
</LinearLayout>
<RelativeLayout
android:id="@+id/sliderAndPoints_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_minBonApps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="0" />
<TextView
android:id="@+id/tv_max_BonApps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="7000" />
<SeekBar
android:id="@+id/sb_changes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/tv_max_BonApps"
android:layout_toRightOf="@+id/tv_minBonApps"
android:max="70"
android:maxHeight="2dip"
android:paddingLeft="8dp"
android:progressDrawable="@drawable/seek_bar"
android:thumb="@drawable/panier_vide_btn" />
</RelativeLayout>
</LinearLayout>
Java文件
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@SuppressLint("NewApi")
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
currentBonsPoints = progress * 100;
if (progress < 3500) {
int xPos = (((seekbar.getRight() - seekbar.getLeft()) * seekbar
.getProgress()) / seekbar.getMax())
+ seekbar.getLeft();
layout_bouns.setPadding(xPos - 25, 0, 0, 0);
bonsCount.setText("" + (progress * 100));
} else if (progress >= 3500 && progress < 4900) {
int xPos = (((seekbar.getRight() - seekbar.getLeft()) * seekbar
.getProgress()) / seekbar.getMax())
+ seekbar.getLeft();
layout_bouns.setPadding(xPos - 34, 0, 0, 0);
bonsCount.setText("" + (progress * 100));
} else {
int xPos = (((seekbar.getRight() - seekbar.getLeft()) * seekbar
.getProgress()) / seekbar.getMax())
+ seekbar.getLeft();
layout_bouns.setPadding(xPos - 37, 0, 0, 0);
bonsCount.setText("" + (progress * 100));
}
}
});
答案 0 :(得分:2)
package com.example.seekbarwithtextviewsample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTxvSeekBarValue;
SeekBar mSkbSample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue);
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample);
mSkbSample.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop());
mTxvSeekBarValue.setVisibility(View.VISIBLE);
}
else if(event.getAction() == MotionEvent.ACTION_MOVE)
{
ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop());
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
mTxvSeekBarValue.setVisibility(View.INVISIBLE);
}
return false;
}
});
}
private void ShowSeekValue(int x, int y)
{
if(x > 0 && x < mSkbSample.getWidth())
{
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y);
mTxvSeekBarValue.setLayoutParams(lp);
mTxvSeekBarValue.setText(""+mSkbSample.getProgress());
}
}
}
并在布局中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Main context -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SeekBar android:id="@+id/skbSample"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="25dp"
android:max="10000">
</SeekBar>
</LinearLayout>
<!-- For display value -->
<AbsoluteLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txvSeekBarValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:background="@drawable/infowindow"
>
</TextView>
</AbsoluteLayout>
</RelativeLayout>
你会得到你想要的......