【问题标题】:How to make my own XML layout view from other views如何从其他视图制作我自己的 XML 布局视图
【发布时间】:2017-05-10 08:19:54
【问题描述】:

我有一个RelativeLayout,它有TextView(第一个标签)、EditText(用于输入)、TextView(第二个标签)。我在我的项目中至少有 10 个活动有这个。我如何提取视图并制作自己的视图。所以,如果我想改变 textSize ,我只需要改变一个地方,而不是 10 处。

例如我想要这个

<RelativeLayout
  android:width="match_parent"
  android:height="wrap_content"
>
  <TextView
    android:id="firstTextView"
    ...
    android:text="I like">
  <EditText
     android:id="edittextColor" 
     hint="type some color here"
     ... >
<TextView
    android:id="secondTextView"
    ...
    android:text="car.">
    </RelativeLayout>

所以,我在很多地方都需要这样的东西。我想要的是:

<MySpecialView
   firstText="I like"
   colorEditTextHint="type color here"
   secondText="car"/>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    充气机

    假设您的 RelativeLayout 文件名为 reusable_layout。这意味着您可以以 R.layout.reusable_layout 的身份访问它(考虑到您将此文件存储在项目的 layouts 文件夹中)。

    在您通常覆盖的 onCreate() 中,在开头添加这些变量:LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE); RelativeLayout layout = inflater.inflate(R.layout.reusable_layout, null);

    之后,拨打setContentView(layout);

    如果你想编辑孩子,你可以打电话给layout.getChildAt(int childNumber); 这会返回一个View

    编辑第一个TextView子的例子:

    TextView tv = (TextView) layout.getChildAt(0);
    tv.setText("Example String");
    

    更新: 另一种做你想做的事的方法!

    创建自定义视图可能会完成这项工作!

    这里有一个很好的教程:https://developer.android.com/training/custom-views/create-view.html#subclassview

    我认为您需要知道的所有内容都包含在其中。 此处将包含另一个可能有用的来源:how to add views inside a custom View?

    希望我能帮上忙,

    -丹尼尔

    【讨论】:

    • 谢谢。我知道这一点,但我仍然需要它作为 Xml
    • 我很高兴,但您为什么需要在 XML 中使用它?创建 Activity 时,您仍然必须执行 setContentView() 吗?因此,它没有任何区别。我对如何做你所要求的事情有一个想法,并且正在研究它。
    • 我需要它,因为 xml 文件的组织更好。我会更新问题
    • 我已经编辑了我的答案,我希望它能回答你的问题。
    【解决方案2】:

    您可以创建一个通用布局,并像这样包含在所有 10 个活动布局中

    common_layout.xml

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical" android:layout_width="match_parent"
       android:layout_height="match_parent">
    
    
      <TextView
        android:id="@+id/label1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Label1"/>
    
      <EditText
        android:id="@+id/input1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label1"
        android:text="Input1"/>
    
     </RelativeLayout>
    

    activity_layout

         <?xml version="1.0" encoding="utf-8"?>
         <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"  
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
            <include layout="@layout/common_layout"/>
    
            <TextView
              android:id="@+id/textinactivity_tv"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Activity text"/>
    
        </LinearLayout>
    

    我希望这是你想要的。

    【讨论】:

      【解决方案3】:

      虽然 Android 提供了各种小部件来提供小而 可重复使用的交互元素,您可能还需要重复使用更大的 需要特殊布局的组件。有效地重复使用 完整的布局,您可以使用 includemerge 标签 在当前布局中嵌入另一个布局。 https://developer.android.com/training/improving-layouts/reusing-layouts.html

      &lt;include&gt; 呢? 在您要添加的任何其他 xml 中创建 your_base_layout.xml&lt;include&gt;

      your_base_layout.xml

      <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/some_other_id">
         <Button
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:id="@+id/button1" />
       </LinearLayout>
      

      <include
         android:id="@+id/include_id"
         layout="@layout/your_base_layout" />
      

      使用示例:another_layout.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/app_bg"
          android:gravity="center_horizontal">
      
            <include
               android:id="@+id/include_id"
               layout="@layout/your_base_layout" />
      
          <TextView android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/hello"
                    android:padding="10dp" />
      
          ...
      
      </LinearLayout>
      

      这是您访问其中视图的方式,

      View includedLayout = findViewById(R.id.some_id_if_needed);
      Button buttonInsideTheIncludedLayout = (Button) includedLayout.findViewById(R.id.button1); // if there is a button in your base layout that you included access like this
      

      找到好答案>here

      【讨论】:

      • 如果我想更改第一个标签文本并使其每个都不同怎么办?
      【解决方案4】:

      您可以使用指定的属性定义自己的控件。

      将 ButtonPlus.java 保存到你的包中。

      例如

      public class ButtonPlus extends Button {
      
          public ButtonPlus(Context context) {
              super(context);
          }
      
          public ButtonPlus(Context context, AttributeSet attrs) {
              super(context, attrs);
              CustomFontHelper.setCustomFont(this, context, attrs);
          }
      
          public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
              CustomFontHelper.setCustomFont(this, context, attrs);
          }
      }
      

      您可以在布局 XML 文件中使用。

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-27
        相关资源
        最近更新 更多