android - Textview 与搜索栏对齐

标签 android textview seekbar

我有一个搜索栏,在其上方放置了一个 TextView ,用于显示搜索栏移动的值。我的问题是 Textview 和 Seekbar 的对齐方式最初是相同的,但随着拖动对齐方式的变化,我附加了屏幕截图和代码。

enter image description here

enter image description here

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

        }
    }
});

最佳答案

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>

你会得到你正在寻找的东西......

关于android - Textview 与搜索栏对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20049548/

相关文章:

java - TextView 中的 Roboto 浅色和 Roboto 粗体

android - 我想播放音频。我使用 Thread、AsyncTask 还是 Service?

android - 自动调整 SeekBars Android

android - 启动谷歌地图 Intent 获取方向需要按 3 次后退按钮才能返回到我的应用程序

android - 按钮数组onclicklistener

android - Android错误 “SQLiteException:near ” 1“: syntax error (code1)

ios - 例如,textView 无法选择?

java - 动画停止后在 MotionEvent 和 Dialog 中使用

android - TextView - 获取可见文本,以及如何将字符串划分为页面?

android - EditText 更新 SeekBar 崩溃