有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓自定义视图,以在Java中动态插入到布局中

我经常强调这一点,我对Android和Java一般来说都是新手:-) 这些xml布局让我头疼

您看到的代码由RelativeLayout中的两个ImageView和两个TextView组成,它们一起构成了一个布局,对我来说,它是一个“自定义按钮”。当我复制并粘贴到我的布局中时,它几乎按照我想要的方式工作

当我需要这样的按钮时,如何在代码中动态地使用xml布局的这一部分,并且仍然能够更改某些属性,例如TextView中的文本

我希望你明白我的意思,我的第一语言不是英语

<RelativeLayout
    安卓:layout_width="wrap_content"
    安卓:layout_height="match_parent" >

    <ImageView
        安卓:id="@+id/myImageView"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"

        安卓:layout_marginTop="20dp"
        安卓:layout_marginLeft="10dp"
        安卓:src="@drawable/box" />

    <TextView
        安卓:id="@+id/myImageViewText"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignBottom="@+id/myImageView"
        安卓:layout_alignLeft="@+id/myImageView"
        安卓:layout_alignRight="@+id/myImageView"
        安卓:layout_alignTop="@+id/myImageView"
        安卓:layout_margin="1dp"
        安卓:gravity="center"
        安卓:text="S-"
        安卓:textColor="#000000"
        安卓:textStyle="bold" />

    <TextView
        安卓:id="@+id/textView1"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignBottom="@+id/myImageViewText"
        安卓:layout_centerHorizontal="true"
        安卓:layout_marginBottom="25dp"
        安卓:text="your turn!"
        安卓:textAppearance="?安卓:attr/textAppearanceSmall" />

    <ImageView
        安卓:id="@+id/imageView2"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_above="@+id/textView1"
        安卓:layout_alignParentTop="true"
        安卓:layout_marginBottom="15dp"
        安卓:layout_marginTop="-10dp"
        安卓:cropToPadding="false"
        安卓:src="@drawable/icon" />

</RelativeLayout>

共 (3) 个答案

  1. # 1 楼答案

    确定开始像这样扩展您自己的视图:

    我确实有一个ButtonImageViewTextView组成的LinearLayout用xml设计:

    XML

    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:adjustViewBounds="true"
            android:padding="3dp"
            android:scaleType="fitStart" />
    
        <TextView
            android:id="@+id/tvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_marginLeft="10dp"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="@android:color/white" />
    
    </merge>
    

    调用ViewObject

    查看菜单按钮

    public class ViewMenuButton extends View{
    
        private TextView tvText;
        private ImageView ivImage;
    
        public ViewMenuButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);        
            init();
        }
        public ViewMenuButton(Context context, AttributeSet attrs) {
            super(context, attrs);      
            init();
        }
        public ViewMenuButton(Context context) {
            super(context);
            init();
        }
    
    
        private void init() {           
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //here you can inflate a own XML for that View
            inflater.inflate(R.layout.view_menubutton, this, true);  
            this.tvText= (TextView)this.findViewById(R.id.tvText);      
            this.ivImage = (ImageView)this.findViewById(R.id.ivImage);
    
        }
    
        public void setText(String text){
            if(this.tvText != null){
                tvText.setText(text);
            }
        }
    
        //... and so on
    
    }
    

    每当您想在xml中使用它时,请确保为视图提供完整的包,如下所示:

    使用XML

    <com.your.package.views.ViewMenuButton
            android:id="@+id/menu_bt_local"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:background="@drawable/action_button"
            android:textSize="@dimen/text_cell"   >
    

    如果您想在代码中使用它,只需如下所示:

    使用JAVA

    LinearLayout rootView = 
      (LinearLayout) findViewById(R.id.mainLayout); //Or sth like this
    ViewMenuButton vmb = new ViewMenuButton(this);
    rootView.add(vmb);
    
    //or if you already have it in XML
    ViewMenuButton vmb = (ViewMenuButton) findViewById(R.id.myVmbtID);
    

    您甚至可以更详细地定义自己的属性以在XML中使用,例如设置Image的源、更改Text、更改TextColor

  2. # 2 楼答案

    创建自定义的View子类。在构造器中膨胀布局,在那里找到必要的视图,并创建必要属性的setter/getter。然后,每次需要这个自定义按钮时,都可以使用这个子类通过代码或xml创建它。如果您还需要能够更改xml中的某些属性,那么您可能需要为自定义视图声明可设置样式的属性。我想你可以找到很多关于如何创建自定义视图的教程

    这是常见的教程Here您可以阅读有关自定义属性的内容。最后here是我上面提到的最好的例子

  3. # 3 楼答案

    您可以膨胀布局并将其添加到其他布局

      RelativeLayout layout = new RelativeLayout(this);
    
      View childButton = getLayoutInflater().inflate(R.layout.child_button, null);
      layout.addView(childButton);
    

    现在您可以在按钮布局中找到子视图,如下所示

     TextView textViewInnerChild = (TextView)childButton.findViewById(R.Id. textInnerView):
    

    然后可以更改内部子视图的值或属性

     textViewInnerChild.set text("your text")