如何在旋转木马下面添加textview

时间:2016-01-07 11:04:58

标签: android android-activity carousel

您好我在我的应用程序中创建了一个旋转木马试图在其下方添加文本视图,我想要特定图像的文本,我想为特定图像添加文本。如何将文字提供给轮播中的图像

   public class Team extends Fragment {

    private HorizontalCarouselStyle mStyle;
    private HorizontalCarouselLayout mCarousel;
    private CarouseAdapter mAdapter;
    private ArrayList<Integer> mData = new ArrayList<Integer>(0);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.team, container, false);

        mData.add(R.drawable.s);
        mData.add(R.drawable.t);
        mData.add(R.drawable.tt);
        mAdapter = new CarouseAdapter(getActivity());
        mAdapter.setData(mData);
        mCarousel = (HorizontalCarouselLayout) view.findViewById(R.id.carousel_layout);
        mStyle = new HorizontalCarouselStyle(getActivity(), HorizontalCarouselStyle.NO_STYLE);
        mCarousel.setStyle(mStyle);
        mCarousel.setAdapter(mAdapter);

        mCarousel.setOnCarouselViewChangedListener(new CarouselInterface() {
            @Override
            public void onItemChangedListener(View v, int position) {
                Toast.makeText(getContext(), "Position " + String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });


        return view;
    }


}

carouseradapter:

public class CarouseAdapter extends BaseAdapter {

    private ArrayList<Integer> mData = new ArrayList<Integer>(0);
    private Context mContext;

    public CarouseAdapter(Context context) {
        mContext = context;
    }

    public void setData(ArrayList<Integer> data) {
        mData = data;
    }

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

    @Override
    public Object getItem(int pos) {
        return mData.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView mImage= new ImageView(mContext);
        mImage.setImageResource(mData.get(arg0));
        mImage.setPadding(5, 5, 5, 5);
        mImage.setBackgroundResource(R.drawable.slider_bg);
        return mImage;
    }

}

布局:

<LinearLayout 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:orientation="vertical"
    android:background="#FFFFFF" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/tb"
            android:textColor="#EA90AF"
            android:layout_gravity="center"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:id="@+id/textView23" />

        <com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
            android:id="@+id/carousel_layout"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

以编程方式在Layout中添加TextView。

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);

// Add textview 
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout.addView(textView1);

希望这有助于您实际需要的东西。