I have created a custom class from a tutorial online. It works fine, but what I want to do is make it so that I can set the text of the view, just like I would any other view. How is that accomplished. Here is the xml for the custom view

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/file_image_holder"
android:layout_width="110dip"
android:layout_height="110dip"
android:src="@drawable/pdf_icon" />
<TextView
android:id="@+id/file_name_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Name Here" />
</LinearLayout>

What I am trying to achieve is this

FileView fileView = new FileView(this)
fileView.setText("text")
有帮助吗?

解决方案

TextView filename = (TextView)findViewById(R.id.file_name_holder);
filename.setText("someFile.exe");

On another note ... you can use hint to give your users a greyed out text prompt that they won't have to erase when entering a filename. In your XML replace android:text with android:hint for your TextView.

其他提示

replace "File Name Here" with "TEXT YOU WANT" or @string/name_of_string_in_string.xml

or the java way, which someone else answered

here no_internet is include xml view . in the include view contain text view. you can set text in include custom text view like ....

 no_internet     = view.findViewById(R.id.no_internet);
  ((TextView) no_internet).setText(R.string.no_internet_connection);

I didnt understand your full requirement. But if you want to create a custom view the following things you should do:

For eg: If you want to create a button using your custom layout, then

  1. Create a class that extends a layout.

    public class MyCustomView extends RelativeLayout {
    
            private Button customBtn;
    
            public MyCustomView(Context context, AttributeSet attrs) {
                 super(context, attrs);
                         init(context);
            }
            private void init(Context context) {
                 customBtn = new Button(context);
                 customBtn.setText("Button");
                 RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                 buttonParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                 buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                 addView(customBtn, buttonParams);
        }
    }
    

If you want to add any listeners, implement and override it.

2.Mention this custom layout inside your xml as: (my MyCustomView is present in com.android package)

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

            <com.android.MyCustomView android:background="@android:color/white" android:id="@+id/myLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:layout_centerHorizontal="true" android:clickable="true" />

    </RelativeLayout>

Please try it out, if you require it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top