访问android中的自定义视图

时间:2012-04-09 12:37:00

标签: android android-layout android-widget

我正在尝试在其下方创建一个图标和简短说明。我创建了一个带有ImageView和TextView的自定义View类,我将它与我的xml文件集成,现在imageView和默认文本出现在屏幕上,我不知道如何更改textview的文本内容或者imageview的来源。

MyView.java

package sda.jk;

public class MyView extends LinearLayout{

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        LayoutInflater li=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v= li.inflate(R.layout.myview, this);
    }

}

myview.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="my view"/>

</LinearLayout>

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/sda.jk"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<sda.jk.MyView
        android:id="@+id/myView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


    </sda.jk.MyView>
</LinearLayout>

5 个答案:

答案 0 :(得分:1)

您在sda.jk.MyView中编写了一个用于设置文本的函数。要更改文本,只需使用字符串参数

调用该函数
 Class MyView extends LinearLayout
 {
     //...

     public void setUserText(String str)
     {
          textview.setText(str);
     }

     //...
 }

答案 1 :(得分:1)

Step-1 首先在myview中为图片视图和文本视图指定ID。 XML

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

    <ImageView 
        android:id="@+id/myImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        />
    <TextView 
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="my view"/>

</LinearLayout>

<强>步骤-2 使用

在oncreate方法中初始化您自己的视图
MyView mv = (MyView)findViewById(R.id.myView1);

<强>步骤-3 使用您自己视图的findViewById方法初始化图像视图和文本视图

TextView textView = (TextView) mv.findViewById(R.id.myText);
        ImageView imageView = (ImageView) mv.findViewById(R.id.myImage);

<强>步骤-4 使用setText方法将文本设置为文本视图

textView.setText("Sunil Kumar Sahoo");

使用

将背景图像设置为图像视图
imageView.setBackgroundResource(R.drawable.ic_launcher);

您的oncreate方法将如下所示(从步骤2到步骤4)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        MyView mv = (MyView)findViewById(R.id.myView1);
        TextView textView = (TextView) mv.findViewById(R.id.myText);
        textView.setText("Sunil Kumar Sahoo");
        ImageView imageView = (ImageView) mv.findViewById(R.id.myImage);
        imageView.setBackgroundResource(R.drawable.ic_launcher);
    }

答案 2 :(得分:0)

TextView中有一个名为compund drawables的有用功能,它允许您在文本的上方,下方,左侧或右侧绘制一个drawable。 像这样使用它:

<TextView
    android:id="@+id/txt"
    android:text="my compound drawable textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/xxxxx" />

你也可以使用drawableBottom,drawableLeft等。 这样你就不需要实现自己的类了。

答案 3 :(得分:0)

您可以通过编写自定义attr.xml并在自定义视图的构造函数中加载属性来添加自定义属性。有关完整示例,请查看此页面 http://javawiki.sowas.com/doku.php?id=android:custom-view-attributes

答案 4 :(得分:0)

要从View层次结构访问单个元素,请使用findViewById方法。请参阅http://developer.android.com/reference/android/view/View.html#findViewById%28int%29

您必须在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/imgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="my view" />

</LinearLayout>

并从您的自定义视图类中引用它们:

ImageView imageView = (ImageView)v.findViewById(R.id.imgView);
TextView textView = (TextView)v.findViewById(R.id.txtView);

通常,您希望将视图保留为实例变量(成员),以便可以从其他方法更改它们。