簡體   English   中英

如何在轉盤下方添加textview

[英]how to add a textview below the 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;
    }


}

輪播適配器:

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>

以編程方式在布局中添加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);

希望這對您的實際需求有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM