【发布时间】:2015-02-03 23:28:27
【问题描述】:
我正在 Android Studio 上编写一个应用程序,我正在尝试使用我需要的特定表单创建一个自定义视图,例如:
(我使用不同的颜色来显示布局应该如何进行)
这是xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="2"
android:background="@android:color/holo_red_dark">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/hourLbl"
android:layout_gravity="center"
android:background="@android:color/holo_blue_bright"/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/minuteLbl"
android:layout_gravity="center"
android:background="@android:color/holo_orange_dark"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/actionName"
android:layout_weight="1"
android:gravity="center"
android:background="@android:color/darker_gray"/>
</LinearLayout>
当我尝试将此视图添加到应用程序中的另一个布局时,问题就出现了。它是一个空白方块。我一直在尝试寻找有关如何正确执行此操作的答案,但没有任何帮助。
这是我想要查看的活动的 xml 中的内容 (ActionView):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".HomeActivity">
<...ActionView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/firstAction" />
</RelativeLayout>
这是我拥有的 ActionView 的构造函数(老实说,不确定如何处理它们,只是从教程中复制它们):
public ActionView(Context context) {
super(context);
}
public ActionView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ActionView,
0, 0);
try {
mHourLbl = a.getString(R.styleable.ActionView_hourLbl);
mMinuteLbl = a.getString(R.styleable.ActionView_minuteLbl);
mActionName = a.getString(R.styleable.ActionView_actionName);
} finally {
a.recycle();
}
}
public ActionView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
【问题讨论】:
-
你对
ActionView的构造函数的实现是什么?问题可能就在那里 -
@blipinsk 你有它。
标签: android android-layout android-studio android-custom-view android-inflate