【问题标题】:how to write text on the image view?如何在图像视图上写文字?
【发布时间】:2013-09-17 06:38:05
【问题描述】:

我需要在图像视图上实现一个文本,用户可以在图像视图上键入他的文本。我们如何根据用户实现文本并根据用户添加字体?

这是我的完整的图像活动课程

     public class FullImageActivity extends Activity {

       @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fullimage);

        // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.mThumbIds[position]);
    }

}

fullimage.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView android:id="@+id/full_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

 </LinearLayout>

【问题讨论】:

标签: android android-layout text fonts android-emulator


【解决方案1】:
<ImageView android:id="@+id/full_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Your Text"
    android:textColor="#000000" />

注意:它只适用于RELATIVE LAYOUT

或者试试下面的代码

TextView t = (TextView)findViewById(R.id.text_view);
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("your text");

对于 EDITEXT

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center_vertical"  >

    <ImageView
        android:id="@+id/full_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <EditText
        android:id="@+id/edit_text1"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

【讨论】:

  • 用户如何在 android 的 TextView 上输入文字?
  • 当我使用相对布局时,图像并不完全适合屏幕,应该根据用户设置文本?
  • 整个屏幕都应该有该图像视图,并且无论用户触摸哪里,edittext 都应该出现,或者用户可以输入文本。这是你想要做的吗???
  • 您在上面输入的内容是正确的。我正在寻找它?
【解决方案2】:

添加相对布局,然后添加 imageview 并在其上添加 textview。

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/icon_favorites" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

【讨论】:

    【解决方案3】:

    使用如下框架布局:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
    <TextView
        android:id="@+id/tvText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Text" Visibility="Gone" />
    
    </FrameLayout>
    

    在代码中,当用户触摸 imageview 上的特定位置时,通过设置 textview 的边距来显示 textview

    【讨论】:

      【解决方案4】:
      // try this
      <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="5dp" >
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:adjustViewBounds="true"/>
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="top" // set gravity as per your requirement to show
                  android:gravity="center_vertical"
                  android:textStyle="bold" />
          </FrameLayout>
      

      【讨论】:

      • 整个屏幕都应该有该图像视图,并且用户触摸的任何地方都可以输入文本???
      【解决方案5】:

      这很好。

      <FrameLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/fb_background" />
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:text="TextView" />
      </FrameLayout>
      

      【讨论】:

      • 用户应该在图像视图的顶部输入自己的文字?
      • 问题是没有显示文本。用户应该使用自定义文本干扰显示自己的文本
      • 你试过 TextView tv=(TextView)findViewById; tv.setText(变量);
      • 你能提供一些你声明的代码吗?
      • 我在全图活动类中定义了
      【解决方案6】:

      根据需要,在RelativeLayout中添加ImageView和EditText:-

      <RelativeLayout android:id="@+id/full_image_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <ImageView android:id="@+id/full_image_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
      
          <EditText android:id="@+id/my_edit_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"  //or any other alignment you want
          android:text="ABCD"
          />
      
      </RelativeLayout>
      

      【讨论】:

      • 当我使用相对布局时,图像并非全部适合全屏,应根据用户在图像视图上键入和设置文本?
      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 2020-12-06
      • 1970-01-01
      • 2013-08-27
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      相关资源
      最近更新 更多