带有视图寻呼机的android动态文本

时间:2013-06-21 10:00:55

标签: android android-viewpager

我在Android应用中实现了一个视图寻呼机。我希望动态文本视图显示在图像视图的顶部和底部。如何为查看寻呼机上的每个图像设置不同的文本?让我知道你的建议。

感谢。

2 个答案:

答案 0 :(得分:10)

您需要为此创建自定义PagerAdapter。我会告诉你一个小小的例子:

您的活动:

public class MainActivity
    extends Activity{

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_main);

    List<CustomObject> items = new ArrayList<CustomObject>();
    items.add(new CustomObject("First Top","First Bot"));
    items.add(new CustomObject("Second Top","Second Bot"));
    items.add(new CustomObject("Third Top","Third Bot"));

    ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
    CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(this,items);
    viewPager.setAdapter(customPagerAdapter);
}}

您的适配器:

public class CustomPagerAdapter
    extends PagerAdapter{

List<CustomObject> items;
LayoutInflater inflater;

public CustomPagerAdapter(Context context, List<CustomObject> items)
{
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.items = items;
}


@Override
public boolean isViewFromObject(View view, Object object)
{
    return view == ((RelativeLayout) object);
}

@Override
public int getCount()
{
    return items.size();
}

    @Override
public void destroyItem(ViewGroup container, int position, Object object)
{
    ((ViewPager) container).removeView((View)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position)
{

    View itemView;
    itemView = inflater.inflate(R.layout.your_item_layout, container, false);

    TextView topTextItem = (TextView) itemView.findViewById(R.id.topText);
    TextView bottomTextItem = (TextView) itemView.findViewById(R.id.bottomText);

    CustomObject customObject = items.get(position);

    topTextItem.setText(customObject.topText);
    bottomTextItem.setText(customObject.bottomText);

    ((ViewPager) container).addView(itemView);

    return itemView;
}}

主要布局 - view_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

布局项目 - your_item_layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

<TextView
    android:id="@+id/topText"
    style="@android:style/TextAppearance.Medium"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Top Text" />


<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@android:drawable/ic_delete" />

<TextView
    style="@android:style/TextAppearance.Medium"
    android:layout_alignParentBottom="true"
    android:id="@+id/bottomText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:gravity="center"
    android:text="Bottom Text" />

祝福。

答案 1 :(得分:0)

您可以在Fragment类中执行此操作

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {    

        View v = inflater.inflate(R.layout.spannerbord_image_new, null);
        // RelativeLayout
        // layout=(RelativeLayout)v.findViewById(R.id.spanner_image_relative);
        Log.v("TEST", "is this printing everytime");
               TextView tv = (TextView ) v.findViewById(R.id.my_tv );
        }

我希望这有效