根据活动中的搜索栏更改视图寻呼机片段中的文本大小

时间:2015-10-28 02:38:48

标签: android android-fragments android-seekbar text-size

在我的活动(Please see image)中,我寻找酒吧和查看寻呼机(有3页)。

我想这样,当活动改变中的搜索栏时,片段大小的文本也会有所不同。

我该怎么做?

我尝试使用界面,但它没有运行。

Mainactivity

package com.creativei.viewpagerloop;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    private static final String LOG_TAG = "ViewPagerLoop";

    public static final String[] content = new String[] {
            "Hello Welcome to ViewPager Loop Example. This is first view. Swipe right → for second view, swipe left � for last view.",
            "Awesome, now you are on second view. Swipe right → again to go to last view.",
            "Finally made it to last view, swipe right → again to go to first view." };
    Interface inter;

    public MainActivity(Interface inter) {
        this.inter = inter;
    }

    private SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        TextView counter = (TextView) findViewById(R.id.counter);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                inter.seekBarChange(progress);
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        SimpleViewPagerAdapter adapter = new SimpleViewPagerAdapter(
                getSupportFragmentManager(), pager, content, counter);
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(adapter);
        pager.setCurrentItem(1, false);
    }

    public static class SimpleFragment extends Fragment implements Interface {
        private TextView textView;

        public SimpleFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            String content = getArguments().getString("content");
            textView = (TextView) rootView.findViewById(R.id.content);
            textView.setText(content);
            return rootView;
        }

        @Override
        public void seekBarChange(int id) {
            // TODO Auto-generated method stub
            textView.setTextSize(id);
        }
    }

    public static class SimpleViewPagerAdapter extends
            FragmentStatePagerAdapter implements OnPageChangeListener {

        private String[] content;
        private ViewPager pager;
        private TextView counter;

        public SimpleViewPagerAdapter(FragmentManager fm, ViewPager pager,
                String[] content, TextView counter) {
            super(fm);
            this.pager = pager;
            this.content = content;
            this.counter = counter;
        }

        @Override
        public Fragment getItem(int position) {
            SimpleFragment fragment = new SimpleFragment();
            Bundle bundle = new Bundle();
            int index = position;

            bundle.putString("content", content[index]);
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return content.length;
        }

        @Override
        public void onPageSelected(int position) {

        }

        private String makeCounterText(int pageNo) {
            return "Page " + pageNo + " of " + content.length;
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

    }
}

接口

package com.creativei.viewpagerloop;

public interface Interface {
    public void seekBarChange(int id);
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.creativei.viewpagerloop.MainActivity"
    tools:ignore="MergeRootFrame" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/counter"
        android:layout_below="@+id/seekBar1" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal" />

</RelativeLayout>

fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.creativei.viewpagerloop.SimpleFragment" >

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以从此示例http://sampleprogramz.com/android/seekbar.php

中获取参考