Java 以编程方式编辑自定义视图

Java 以编程方式编辑自定义视图,java,android,xml,android-layout,android-custom-view,Java,Android,Xml,Android Layout,Android Custom View,我创建了如下布局: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rlMenu" android:layout_centerHorizontal="true" android:gravity="center" android:layout_centerVertical="true

我创建了如下布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rlMenu"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:layout_centerVertical="true">

    <RelativeLayout
        android:background="@drawable/dark_rectangle_bord"
        android:id="@+id/rl1dia"
        android:elevation="10dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_width="@dimen/sizeCard"
        android:layout_height="@dimen/sizeCard"
        android:layout_marginBottom="@dimen/padding_bottom_cards"
        android:layout_marginEnd="@dimen/padding_end_cards"
        android:layout_marginRight="@dimen/padding_end_cards"
        android:layout_marginLeft="@dimen/padding_start_cards"
        android:layout_marginStart="@dimen/padding_start_cards"
        android:layout_marginTop="@dimen/padding_top_cards">

        <TextView
            android:text="1º Dia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1dia"
            android:textStyle="normal|bold"
            android:textColor="@color/Branco"
            android:layout_alignParentBottom="true"
            android:padding="10dp"
            android:textSize="@dimen/texto_pequeno"
            android:gravity="center"
            android:fontFamily="sans-serif"
            android:layout_centerHorizontal="true"/>

        <ImageView
            app:srcCompat="@drawable/ic_calendario_1"
            android:id="@+id/iv1dia"
            android:layout_width="@dimen/sizeImage"
            android:layout_height="@dimen/sizeImage"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            />
如何以编程方式更改
文本视图
相对视图
、背景和
图像视图

  CustomView cv = new CustomView(MainActivity.this);
  cv.setImage(R.drawable.chips);
  cv.setRlBackground(Color.WHITE);
  cv.setText("Hello, World!");

你可以用LayoutInflater

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mView = (View) inflater.inflate(R.layout.custom_view, null);
    View email = (View) mView.findViewById(R.id.email);
    yourParentView.addView(mView);

但是,请确保在将视图添加到其父调用中时,您的ParentView的父调用
removeAllview()
方法

可能您想要创建一个实际的自定义
视图
,使用include不会创建自定义
视图
,而只会在布局中注入特定的xml。因此,创建一个类(在本例中扩展了
RelativeLayout
,但您可以在这里使用任何适合您的方法)

自定义视图布局的xml(
custom\u View.xml
),您可能希望使用
merge
而不是
include
,因为您已经有了父布局(
RelativeLayout

在您的
活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.my_activity);

    CustomView customView = (CustomView) findViewById(R.id.custom_view);
    customView.setImage(R.drawable.some_icon);
    customView.setText("Hello world");
}

您应该直接在mainactivity中创建布局,而不是在其他地方创建布局,然后将其包括在内。
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mView = (View) inflater.inflate(R.layout.custom_view, null);
    View email = (View) mView.findViewById(R.id.email);
    yourParentView.addView(mView);
public class CustomView extends RelativeLayout {

    private TextView textView;
    private ImageView imageView;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.custom_view, this);

        imageView = (ImageView) findViewById(R.id.image_view);
        textView = (TextView) findViewById(R.id.text_view);
    }

    public void setImage(int resId){
        imageView.setImageResource(resId);
    }

    public void setText(String text){
        textView.setText(text);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
     <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="48dp"
        android:layout_height="48dp"
        />
</merge>
<?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_main"
    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">

    <com.example.lelloman.dummy.CustomView
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.my_activity);

    CustomView customView = (CustomView) findViewById(R.id.custom_view);
    customView.setImage(R.drawable.some_icon);
    customView.setText("Hello world");
}