【问题标题】:Custom Views, to insert them dynamically to Layout in Java自定义视图,将它们动态插入到 Java 中的布局中
【发布时间】:2014-04-09 11:14:27
【问题描述】:

我怎么强调都不过分,总的来说,我是 Android 和 Java 的新手 :-) 而这些 xml 布局让我很头疼。

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

当我需要这样的按钮并且仍然能够更改某些属性(例如 textviews 中的文本)时,如何在我的代码中动态使用这部分 xml-layout?

希望你明白我的意思,我的母语不是英语。

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

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

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

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

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

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

</RelativeLayout>

【问题讨论】:

  • 你明白我的回答吗?

标签: java android android-layout


【解决方案1】:

好的,开始像这样扩展您自己的视图:

我确实有一个ButtonImageView 和一个TextView 在一个用xml 设计的LinearLayout 制成:

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 调用

ViewMenuButton

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 中使用它时,请确保给 View 提供完整的包,如下所示:

使用 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 等pp Tutoiral

【讨论】:

    【解决方案2】:

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

    Here是常用教程。 Here 你可以阅读自定义属性。最后here 是我上面提到的最好的例子。

    【讨论】:

      【解决方案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")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 2016-03-28
        • 2015-09-17
        • 1970-01-01
        • 2011-03-16
        • 1970-01-01
        相关资源
        最近更新 更多