2016-01-07 28 views
0

嗨,我在我的應用程序中創建了一個旋轉木馬,試圖在它下面添加文本視圖,我想要特定圖像的文本,我想爲特定圖像添加文本。我怎麼給我的文字到圖像的旋轉木馬如何在旋轉木馬下方添加文本視圖

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> 
+0

爲什麼不在xml文件中添加「TextView」,然後根據傳送帶圖像更新'onItemChangedListener'中的值? – Rami

+0

我已經嘗試過,但我不知道如何在傳送帶移動時更改文本。我們如何才能在onclicklistener – sun

+0

中給出該圖像的特定位置的文本在'onItemChangedListener'中,您具有圖像的位置,所以您只需要獲取此圖像的文本。您可以使用exp來存儲文本的'ArrayList',然後您只需調用'mTextsList.get(position);' – Rami

回答

0

在佈局中添加一個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); 

希望這可以幫助您實際需要。

+0

我想添加textview動態添加imageview – sun

+1

然後請改變你的標題,因爲它沒有描述你實際需要什麼。 – Kristo