【问题标题】:Edit Custom Views programmatically以编程方式编辑自定义视图
【发布时间】:2017-06-18 04:47:45
【问题描述】:

我创建了这样的布局:

<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"
            />

在那之后,我将它包含在我的 MainActivity 中。

    <include
    android:id="@+id/my_custom_view"
    layout="@layout/custom_view></include>

但是在这个自定义视图中有一个RelativeLayout、一个ImageView 和一个TextView。我需要动态创建一些自定义视图,示例如下。

如何以编程方式创建此自定义视图? 例如:

   Button bt = new Button(MainActivity.this);

我怎样才能以编程方式更改TextViewRelativeLayout、背景和ImageView喜欢:

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

【问题讨论】:

  • 你应该直接在 mainactivity 中进行布局,而不是在其他地方然后包含它。

标签: java android xml android-layout android-custom-view


【解决方案1】:

您可以为此使用 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);

但请确保当您将视图添加到其父视图时调用 removeAllview() yourParentView 的方法

【讨论】:

    【解决方案2】:

    也许您想创建一个实际的自定义View,使用include 不会创建自定义View,而只会在您的布局中注入特定的xml。所以,创建一个类(在这种情况下扩展 RelativeLayout 但你可以在这里使用任何适合你的东西)

    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);
        }
    }
    

    您的自定义视图 (custom_view.xml) 的布局的 xml,您可能希望使用 merge 而不是 include,因为您已经有一个父布局(在这种情况下为 RelativeLayout

    <?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>
    

    像这样将您的自定义视图添加到Activity 布局中,请注意您需要使用您正在使用的自定义View 类的全名,在本例中为com.example.lelloman.dummy.CustomView

    <?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>
    

    在你的Activity

    @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");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 2016-07-02
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2012-12-02
      相关资源
      最近更新 更多