3

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

谢谢。

4

2 回答 2

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" />

最好的祝愿。

于 2013-06-21T10:14:10.833 回答
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 );
        }

我希望这有效

于 2013-06-21T10:13:30.600 回答