如何在`PhotoView`上显示文字?

问题描述:

在我的应用程序中,我使用名为PhotoView的库在全屏幕(实际上是一项新活动)中显示图像。示例图像位于下方,因此我需要将文本显示为如此。如何在`PhotoView`上显示文字?

enter image description here

我已经发布了XML代码,请检查下面

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_view_full_screen_image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.theace.fullscreen.ViewFullScreenImage" 
    android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/li1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

       <com.github.chrisbanes.photoview.PhotoView 
        android:id="@+id/photo_view" 
        android:layout_width="match_parent" 
        android:layout_centerInParent="true" 
        android:layout_height="match_parent" 
        android:layout_marginBottom="40dp" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="30dp" 
        android:layout_marginBottom="10dp" 
        android:textColor="@android:color/white" 
        android:id="@+id/tv1"/> 


     </LinearLayout> 


</LinearLayout> 

下面是我相关的Java代码

package com.example.theace.fullscreen; 

import android.graphics.Color; 
import android.net.Uri; 
import android.support.v4.app.NavUtils; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MenuItem; 
import android.view.Window; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import com.github.chrisbanes.photoview.PhotoView; 
import com.squareup.picasso.Picasso; 

public class ViewFullScreenImage extends AppCompatActivity { 

    RelativeLayout li1; 
    TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_view_full_screen_image); 
     li1 = (RelativeLayout) findViewById(R.id.activity_view_full_screen_image); 
     li1.setBackgroundColor(Color.BLACK); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     textView = (TextView)findViewById(R.id.tv1); 

     loadImage(); 
    } 

    public void loadImage(){ 

     Uri imageUrl = Uri.parse("https://s-media-cache-ak0.pinimg.com/736x/39/c7/64/39c764abd350c509f76c6362780a3a78--outdoor-portraits-outdoor-portrait-photography.jpg"); 

     PhotoView photoView = (PhotoView) findViewById(R.id.photo_view); 
     Picasso.with(this).load(imageUrl).into(photoView); 
     textView.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tristique nunc turpis, at congue est molestie a. Etiam nec cursus arcu. Curabitur vestibulum ligula vitae velit rutrum sollicitudin. Phasellus imperdiet ultrices semper. Morbi laoreet magna sit amet mi vulputate pharetra. Proin faucibus maximus massa, sit amet vestibulum elit posuere eget."); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       NavUtils.navigateUpFromSameTask(this); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

我该如何解决这个问题?

+0

关于向下票和关闭对这个问题投票,所以“方式”是告诉你为什么你投票或接近投票,而不仅仅是点击一个按钮并运行。我认为没有理由进行反对票或近距离投票。所以这些“肇事逃逸”的人真的需要调查小组。 –

+0

@PeakGen:你被误导了。 SO方式是人们可以为他们喜欢的任何理由进行投票,并且他们可以以匿名方式进行投票,只要他们不关注一个用户。在_Meta Stack Overflow_上有很多改变这个方案的建议都失败了。 – halfer

只需使用下面的代码

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_view_full_screen_image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.theace.fullscreen.ViewFullScreenImage" 
    android:orientation="vertical"> 


     <com.github.chrisbanes.photoview.PhotoView 
      android:id="@+id/photo_view" 
      android:layout_width="match_parent" 
      android:layout_centerInParent="true" 
      android:layout_height="match_parent" 
      android:scaleType="center"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_alignParentBottom="true" 
      android:textColor="@android:color/white" 
      android:layout_gravity="bottom|center_horizontal" 
      android:id="@+id/tv1"/> 


</FrameLayout> 

试试这个让你activity_view_full_screen.xml这样

使用RelativeLayout,让你TextViewandroid:layout_alignParentBottom="true"

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_view_full_screen_image" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 


<com.github.chrisbanes.photoview.PhotoView 
    android:id="@+id/photo_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:scaleType="fitXY" 
    android:layout_marginBottom="40dp" 
    android:src="@drawable/disha" /> 



    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:gravity="center" 
     android:padding="5dp" 
     android:layout_alignParentBottom="true" 
     android:background="#9a080808" 
     android:text="@string/abc" 
     android:textColor="@android:color/white" /> 


</RelativeLayout> 
+0

对不起,但这不是解决方案。您正在图像下方显示文本,使图像显示在文本之上而不管文字是什么。我还尝试添加50个单词的长文本,大部分文本只是不可见。文字应该在图像底部(如果图像足够大) –

+0

没有给你..? –

+0

文本应该像我提供的截图一样。请检查,那里的文字不在图像下方,而是在图像的底部。截图取自WhatsApp。在那里,如果图像不够“足够大”以显示FULLSCREEN,那么在黑色背景中图像的正下方会显示文本,否则将显示文本,如截图所示。 –