Android TextView与ImageView的字幕

Android TextView与ImageView的字幕,android,textview,imageview,marquee,Android,Textview,Imageview,Marquee,我需要有文本视图和图像视图的字幕显示在图像中。 如果单击textview或Imageview,则应打开一个活动。有人能提出一个办法吗 ImageView应该是可点击的。我在HorizontalView中实现了图像视图上的选框,与您在问题中描述的类似,使用 对于您的问题,您必须在HorizontalView内的一个布局中使用texview和Imagview,并在HorizontalView上应用字幕 签出代码# 我有固定数量的图像要显示。要处理click事件,请查看下面的代码 public

我需要有文本视图和图像视图的字幕显示在图像中。 如果单击textview或Imageview,则应打开一个活动。有人能提出一个办法吗


ImageView应该是可点击的。

我在HorizontalView中实现了图像视图上的选框,与您在问题中描述的类似,使用

对于您的问题,您必须在HorizontalView内的一个布局中使用
texview
Imagview
,并在HorizontalView上应用字幕

签出代码#

我有固定数量的图像要显示。要处理click事件,请查看下面的代码

    public void onClick(View v) {
    for (int j = 0; j < 25; j++) {
        if (v == imgv[j]) {

            //do something
        }
    }

}
public void onClick(视图v){
对于(int j=0;j<25;j++){
如果(v==imgv[j]){
//做点什么
}
}
}
已创建自定义视图

public class MarqueeLayout extends ViewGroup {

private static final int VERTICAL_SPACING = 10;
private static final int HORIZONTAL_SPACING = 10;
private static final String TAG = MarqueeLayout.class.getName();
private int line_width;
private List<View> views; 
private Timer timer;

private int scrollX = 0;

public MarqueeLayout(Context context)
{
    super(context);
}
private Handler handler;
private int index = 0;
private int childCount;
public MarqueeLayout(Context context, AttributeSet attrs)
{
    super(context, attrs);
    handler = new Handler();
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    requestLayout();
                }
            });
        }
    }, 1000, 200);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
    if(views == null) {
        views = new ArrayList<View>();
        childCount = getChildCount();
        for(int i = 0; i < childCount; i++) {
            views.add(getChildAt(i));
        }

    }
    final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
    int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
    final int count = getChildCount();
    int line_height = 0;

    int xpos = getPaddingLeft();
    int ypos = getPaddingTop();

    int childHeightMeasureSpec;
    if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)
    {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
    }
    else
    {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    for (int i = 0; i < count; i++)
    {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE)
        {
            child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
            final int childw = child.getMeasuredWidth();
            line_height = Math.max(line_height, child.getMeasuredHeight() + VERTICAL_SPACING);

            xpos += childw + HORIZONTAL_SPACING;
        }
    }
    this.line_width = xpos;

    setMeasuredDimension(width, height);
}

@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams()
{
    return new LayoutParams(1, 1); 
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
{
    if (p instanceof LayoutParams)
    {
        return true;
    }
    return false;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    Log.d(TAG, "onLayout called");
    int count = getChildCount();
    final int width = r - l;

    scrollX -= 20;
    if(line_width + scrollX < 0) {
        scrollX = 0;
    }
    int i = 0;
    while(count > 0) {
        View c = getChildAt(i);
        if(c == null) {
            break;
        }
        int w = c.getMeasuredWidth();
        Log.d(TAG, "scrollX : " + scrollX + " width : " + w);
        if(scrollX < -w) {
            this.removeViewAt(0);
            scrollX += w;
        } else {
            break;
        }
        i++;
        count--;
    }
    count = getChildCount();
    int xpos = getPaddingLeft() + scrollX;
    int ypos = getPaddingTop();
    for (i = 0; i < count; i++)
    {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE)
        {
            final int childw = child.getMeasuredWidth();
            final int childh = child.getMeasuredHeight();
            child.layout(xpos, ypos, xpos + childw, ypos + childh);
            xpos += childw + HORIZONTAL_SPACING;
        }
    }
    while(xpos < getWidth()) {
        View v = views.get(index % childCount);
        addView(v);
        xpos += v.getMeasuredWidth();
        index++;
    }
}} 
公共类MarqueeLayout扩展了视图组{
专用静态最终int垂直_间距=10;
专用静态最终int水平_间距=10;
私有静态最终字符串标记=MarqueeLayout.class.getName();
私有整数线宽;
私人列表视图;
私人定时器;
私有整数x=0;
公共选框布局(上下文)
{
超级(上下文);
}
私人经办人;
私有整数指数=0;
私人int儿童计数;
公共选框布局(上下文、属性集属性)
{
超级(上下文,attrs);
handler=新的handler();
定时器=新定时器();
timer.schedule(新TimerTask(){
@凌驾
公开募捐{
handler.post(新的Runnable(){
@凌驾
公开募捐{
requestLayout();
}
});
}
}, 1000, 200);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级)
{
断言(MeasureSpec.getMode(widthmasurespec)!=MeasureSpec.UNSPECIFIED);
如果(视图==null){
视图=新的ArrayList();
childCount=getChildCount();
for(int i=0;i0){
视图c=getChildAt(i);
如果(c==null){
打破
}
int w=c.getMeasuredWidth();
Log.d(标签“scrollX:+scrollX+”宽度:+w);
如果(滚动X<-w){
此.removeViewAt(0);
x+=w;
}否则{
打破
}
i++;
计数--;
}
count=getChildCount();
int xpos=getPaddingLeft()+scrollX;
int ypos=getPaddingTop();
对于(i=0;i
您可以在这个布局中拥有任何类型的视图,您可以在xml中调用这个布局

<Your.Package.name.MarqueeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is textview 1"
        tools:context=".MainActivity" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clicked"
        android:text="Click me 1" />

</Your.Package.name.MarqueeLayout>

  • 我在android上使用了TextView:ellipsize=“marquee”。它工作得很好,但缺点之一是当marque继续运行时,堆大小会在10到20分钟后增加,应用程序会崩溃。所以我使用了RecycleView选项。它工作得非常好,并且不增加堆大小

  • 可以使用recycleView在marque中设置文本和图像。我浪费了两天终于找到了正确的解决方案。按此步骤操作

步骤1:在片段或活动布局中使用recycleView

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/marqueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:clipToPadding="false" />

 </LinearLayout>
最后,它看起来像下面的屏幕短

public class MarqueeLayout extends ViewGroup {

private static final int VERTICAL_SPACING = 10;
private static final int HORIZONTAL_SPACING = 10;
private static final String TAG = MarqueeLayout.class.getName();
private int line_width;
private List<View> views; 
private Timer timer;

private int scrollX = 0;

public MarqueeLayout(Context context)
{
    super(context);
}
private Handler handler;
private int index = 0;
private int childCount;
public MarqueeLayout(Context context, AttributeSet attrs)
{
    super(context, attrs);
    handler = new Handler();
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    requestLayout();
                }
            });
        }
    }, 1000, 200);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
    if(views == null) {
        views = new ArrayList<View>();
        childCount = getChildCount();
        for(int i = 0; i < childCount; i++) {
            views.add(getChildAt(i));
        }

    }
    final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
    int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
    final int count = getChildCount();
    int line_height = 0;

    int xpos = getPaddingLeft();
    int ypos = getPaddingTop();

    int childHeightMeasureSpec;
    if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)
    {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
    }
    else
    {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }

    for (int i = 0; i < count; i++)
    {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE)
        {
            child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
            final int childw = child.getMeasuredWidth();
            line_height = Math.max(line_height, child.getMeasuredHeight() + VERTICAL_SPACING);

            xpos += childw + HORIZONTAL_SPACING;
        }
    }
    this.line_width = xpos;

    setMeasuredDimension(width, height);
}

@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams()
{
    return new LayoutParams(1, 1); 
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
{
    if (p instanceof LayoutParams)
    {
        return true;
    }
    return false;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    Log.d(TAG, "onLayout called");
    int count = getChildCount();
    final int width = r - l;

    scrollX -= 20;
    if(line_width + scrollX < 0) {
        scrollX = 0;
    }
    int i = 0;
    while(count > 0) {
        View c = getChildAt(i);
        if(c == null) {
            break;
        }
        int w = c.getMeasuredWidth();
        Log.d(TAG, "scrollX : " + scrollX + " width : " + w);
        if(scrollX < -w) {
            this.removeViewAt(0);
            scrollX += w;
        } else {
            break;
        }
        i++;
        count--;
    }
    count = getChildCount();
    int xpos = getPaddingLeft() + scrollX;
    int ypos = getPaddingTop();
    for (i = 0; i < count; i++)
    {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE)
        {
            final int childw = child.getMeasuredWidth();
            final int childh = child.getMeasuredHeight();
            child.layout(xpos, ypos, xpos + childw, ypos + childh);
            xpos += childw + HORIZONTAL_SPACING;
        }
    }
    while(xpos < getWidth()) {
        View v = views.get(index % childCount);
        addView(v);
        xpos += v.getMeasuredWidth();
        index++;
    }
}} 
<Your.Package.name.MarqueeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is textview 1"
        tools:context=".MainActivity" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clicked"
        android:text="Click me 1" />

</Your.Package.name.MarqueeLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/marqueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:clipToPadding="false" />

 </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_up"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="One"
        />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_down"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Two"
       />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_up"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Three"
        />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/arrow_down"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="18sp"
        android:text="Four"
     />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

</LinearLayout>
public class MarqueAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public class ViewHolderItem extends RecyclerView.ViewHolder {
    public ViewHolderItem(View view) {
        super(view);
 }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.marque_adapter, parent, false);
    viewHolder = new ViewHolderItem(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}

@Override
public int getItemCount() {
    // You can make count infinite time
    return 1000;
}}
  private final Handler mHandler = new Handler(Looper.getMainLooper());
  private final Runnable SCROLLING_RUNNABLE = new Runnable() {
    @Override
    public void run() {
        final int duration = 20;
        final int pixelsToMove = 20;
        mRecycleMarqueList.smoothScrollBy(pixelsToMove, 0);
        mHandler.postDelayed(this, duration);
    }
};

RecyclerView mRecycleMarqueList = (RecyclerView) view.findViewById(marqueList);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecycleMarqueList.setLayoutManager(layoutManager);
    MarqueAdapter mMarqueAdapter = new MarqueAdapter();
    mRecycleMarqueList.setAdapter(mMarqueAdapter);
    mHandler.post(SCROLLING_RUNNABLE);