2016-12-25 27 views
1

我有一个RelativeLayout,它有TextView(第一个标签),EditText(用于输入),TextView(第二个标签)。我在我的项目中至少有10项活动。我如何提取视图并制作我自己的视图。所以,如果我想改变TEXTSIZE,我将不得不改变它在一个地方,而不是10如何从其他视图制作我自己的XML布局视图

比如我想有这个

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

回答

1

Inflaters

让我们假设你的RelativeLayout文件被称为reusable_layout。这意味着您可以将其作为R.layout.reusable_layout进行访问(考虑将此文件存储在项目的布局文件夹中)。

在你平常的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"); 

UPDATE: 另一种方式做你想要的!

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

上是个不错的教程这里包括:https://developer.android.com/training/custom-views/create-view.html#subclassview

我认为,所有你需要知道的是包含在。 另一个可能有用的来源将包括在这里:how to add views inside a custom View?

希望我帮助,

-Daniel

+0

谢谢。我知道这一点,但我仍然需要它作为Xml –

+0

我很高兴,但为什么你需要它在XML?当Activity创建正确时,你还必须做setContentView()吗?因此它没有区别。我对如何去做你所要求的并正在研究它有一个想法。 – BlazeDev

+0

我需要它,因为更好地组织了xml文件。我将更新问题 –

0

您可以创建一个共同的布局,包括在所有的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> 

我希望这是你想要的。

0

虽然Android提供了一个各种各样的工具,以提供小型和 可重复使用的互动元素,您可能还需要重新使用需要特殊的布局大 组件。要有效地重新使用 完整的布局,可以使用包括合并标签到 嵌入当前布局的另一个布局。 https://developer.android.com/training/improving-layouts/reusing-layouts.html

什么<include> 创建你your_base_layout.xml和要添加它

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" /> 
<include>它在任何地方其他XML

示例o ˚F用法: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

+0

如果我想更改第一个标签文本并使它们每个都不同,该怎么办? –

0

您可以指定属性的定义自己的控制。

将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文件中使用。

相关问题