简体   繁体   中英

How to resize TextView with SeekBar's progress (Android)

I'm triying to change the text size of a TextView using a SeekBar, and it's works fine ONLY when i increase the size, when decrease, the "text size" changes well, but the "textview's layout" seems to have the biggest height you put (The text appears in the center of the layout as if has the match_parent property instead of wrap_content).

I tried many combinations of the textview's layout properties (It's in a RelativeLayout) (H:WRAP_CONT,W:WRAP_CONT / H:MATCH_PAR,W:MATCH_PAR,ALING PARENT TOP TRUE /.....)

Can someone help me??

EDIT: The textview should appear on top

LAYOUT

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/preview_preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/preview_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent" android:background="@drawable/cuadros_001"/>

            <TextView
                android:id="@+id/preview_text_top"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="TextView" 
                android:textColor="#FFFFFF"/>

            <TextView
                android:id="@+id/preview_text_bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2.5dp"
                android:gravity="bottom|center"
                android:text="PREVIEW_TEXT_BOTTOM"
                android:textColor="#FFFFFF"
                android:textSize="25dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>

           </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/preview_gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/backgroundtransparencia"
            android:gravity="center"
            android:horizontalSpacing="5dp"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" >

        </GridView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/preview_child_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</ViewFlipper>

---SEEKBAR--

seekbar_toptext =(SeekBar)findViewById(R.id.seekbar_toptext);
    seekbar_toptext.setProgress(40);
    seekbar_toptext.incrementProgressBy(05);
    seekbar_toptext.setMax(100);

    seekbar_toptext.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        int topsize;

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub


        }


        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

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

            preview_text_top.setTextSize(TypedValue.COMPLEX_UNIT_SP ,progress);

        }
    });

Thanks in advance.

Try this Code...

I hope this will helps to you...

final TextView t1=new TextView(this); 
t1.setText("Hello Android");        
    final SeekBar sk=(SeekBar) findViewById(R.id.seekBar1);     
    sk.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      
    }       

    @Override       
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
        // TODO Auto-generated method stub      

        t1.setTextSize(progress);
        Toast.makeText(getApplicationContext(), String.valueOf(progress),Toast.LENGTH_LONG).show();

    }       
});             

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM