编写一个按钮以在我的项目的各个部分使用
Compose a button to use in various parts of my project
我有一个按钮,我需要在我的所有项目中使用具有相同属性的同一个按钮,我想创建一个组件来调用这个组件,如果我需要更改我的按钮,只需更改一个class 改变所有用途。
我有这个按钮:
<br.com.simplepass.loading_button_lib.customViews.CircularProgressButton
android:id="@+id/createMatch"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@color/buttonColor"
android:layout_marginTop="16dp"
app:spinning_bar_width="4dp"
app:spinning_bar_color="#FFF"
android:alpha="0.5"
android:textColor="@color/colorAccent"
android:elevation="4dp"
android:enabled="false"
android:layout_marginLeft="16dp"
android:textStyle="bold"
android:text="@string/createMatch"
android:textSize="14sp"
app:spinning_bar_padding="6dp"/>
我创建了一个 class 扩展 CircularProgressButton,但我不知道如何在代码中访问一些属性。 app的属性:例如.
合并两个问题,如何制作具有各种元素的布局的组件?
例如:
<LinearLayout>
<Toolbar/>
<LinearLayout/>
</LinearLayout>
我想在 class 中编写此 xml 以使用我的自定义 class.
在所有布局中调用并获取所有这些元素
<MyLinearLayout>
...
</MyLinearLayout>
some proprieties i don't have ideia how to access in the code. The proprieties with app: for example.
Have a look here.
您将必须子类化一个 View 并提供一个采用 Context 和 AttributeSet 的构造函数。
class CircleView extends View {
private String mTitleText = "Your default title";
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
// You can and should also pass a style as third argument
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0);
if (a.hasValue(R.styleable.CircleView_titleText)) {
mTitleText = a.getString(R.styleable.CircleView_titleText);
}
a.recycle();
}
}
您可能已经注意到在我的示例中使用了 R.styleable.CircleView_titleText
。在使用自定义属性之前,您必须将它们告知编译器。这是通过添加 .xml
来定义您的属性及其预期格式来实现的。
<resources>
<declare-styleable name="CircleView">
<attr name="titleText" format="string" />
</declare-styleable>
</resources>
how i make a component with a layout with various itens?
Reusable Layouts 可能就是您要找的。
这方面的一个示例是将自定义工具栏定义为 toolbar.xml
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e40056"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="#ffffff"
/>
然后include
它在其他视图中:
<include
android:id="@+id/myToolbarId"
layout="@layout/toolbar"
/>
如果您查看 ProgressBar 的源代码,您会发现可以使用 TypedArray
从 XML AttributeSet
.
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
...
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
...
// this is where we get the drawable that is specified in the XML
final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
...
}
您可以执行相同的操作来获取您要查找的 app
属性,但某些属性被标记为内部属性。在这些情况下,我会考虑在 res/values/attrs.xml
中覆盖它们,因为它们可能会改变。
注意:progressDrawable不一定是内部的,我只是拿来举例
<resources>
<declare-styleable name="ProgressBar">
<attr name="progressDrawable"/>
</declare-styleable>
</resources>
关于你的第二个问题,我不确定你是想扩展 LinearLayout
(我不建议这样做,因为你必须编写代码来创建视图)还是简单地重用LinearLayout
及其内部的视图。
如果是后者,那么你可以只用你的 LinearLayout
和适当的 children 创建一个 XML 文件,我们称它为 myLayout.xml
.然后,在您的其他 XML 文件中,您可以像这样放置它:
myLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<Toolbar/>
<LinearLayout/>
</LinearLayout>
activity_main.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lorem_ipsum"/>
<include layout="@layout/myLayout"/>
</LinearLayout>
我有一个按钮,我需要在我的所有项目中使用具有相同属性的同一个按钮,我想创建一个组件来调用这个组件,如果我需要更改我的按钮,只需更改一个class 改变所有用途。
我有这个按钮:
<br.com.simplepass.loading_button_lib.customViews.CircularProgressButton
android:id="@+id/createMatch"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@color/buttonColor"
android:layout_marginTop="16dp"
app:spinning_bar_width="4dp"
app:spinning_bar_color="#FFF"
android:alpha="0.5"
android:textColor="@color/colorAccent"
android:elevation="4dp"
android:enabled="false"
android:layout_marginLeft="16dp"
android:textStyle="bold"
android:text="@string/createMatch"
android:textSize="14sp"
app:spinning_bar_padding="6dp"/>
我创建了一个 class 扩展 CircularProgressButton,但我不知道如何在代码中访问一些属性。 app的属性:例如.
合并两个问题,如何制作具有各种元素的布局的组件?
例如:
<LinearLayout>
<Toolbar/>
<LinearLayout/>
</LinearLayout>
我想在 class 中编写此 xml 以使用我的自定义 class.
在所有布局中调用并获取所有这些元素<MyLinearLayout>
...
</MyLinearLayout>
some proprieties i don't have ideia how to access in the code. The proprieties with app: for example.
Have a look here. 您将必须子类化一个 View 并提供一个采用 Context 和 AttributeSet 的构造函数。
class CircleView extends View {
private String mTitleText = "Your default title";
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
// You can and should also pass a style as third argument
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0);
if (a.hasValue(R.styleable.CircleView_titleText)) {
mTitleText = a.getString(R.styleable.CircleView_titleText);
}
a.recycle();
}
}
您可能已经注意到在我的示例中使用了 R.styleable.CircleView_titleText
。在使用自定义属性之前,您必须将它们告知编译器。这是通过添加 .xml
来定义您的属性及其预期格式来实现的。
<resources>
<declare-styleable name="CircleView">
<attr name="titleText" format="string" />
</declare-styleable>
</resources>
how i make a component with a layout with various itens?
Reusable Layouts 可能就是您要找的。
这方面的一个示例是将自定义工具栏定义为 toolbar.xml
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e40056"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="#ffffff"
/>
然后include
它在其他视图中:
<include
android:id="@+id/myToolbarId"
layout="@layout/toolbar"
/>
如果您查看 ProgressBar 的源代码,您会发现可以使用 TypedArray
从 XML AttributeSet
.
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
...
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
...
// this is where we get the drawable that is specified in the XML
final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
...
}
您可以执行相同的操作来获取您要查找的 app
属性,但某些属性被标记为内部属性。在这些情况下,我会考虑在 res/values/attrs.xml
中覆盖它们,因为它们可能会改变。
注意:progressDrawable不一定是内部的,我只是拿来举例
<resources>
<declare-styleable name="ProgressBar">
<attr name="progressDrawable"/>
</declare-styleable>
</resources>
关于你的第二个问题,我不确定你是想扩展 LinearLayout
(我不建议这样做,因为你必须编写代码来创建视图)还是简单地重用LinearLayout
及其内部的视图。
如果是后者,那么你可以只用你的 LinearLayout
和适当的 children 创建一个 XML 文件,我们称它为 myLayout.xml
.然后,在您的其他 XML 文件中,您可以像这样放置它:
myLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<Toolbar/>
<LinearLayout/>
</LinearLayout>
activity_main.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lorem_ipsum"/>
<include layout="@layout/myLayout"/>
</LinearLayout>