我创建了一个自定义视图(复合视图),它继承自FrameLayout并包含多个子视图:
MediaComponentView.java:
final public class MediaComponentView extends FrameLayout {
private int ratio = 1;
private ImageView imageView;
private CircularProgressView progressView;
private View downloadButton;
private View cancelButton;
private View playButton;
public MediaComponentView(Context context) {
super(context);
initializeViews();
}
public MediaComponentView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeViews();
}
public MediaComponentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initializeViews();
}
private void initializeViews() {
inflate(getContext(), R.layout.view_media_component, this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
imageView = (ImageView) this.findViewById(R.id.image_view);
progressView = (CircularProgressView) …Run Code Online (Sandbox Code Playgroud) android android-custom-view layout-inflater android-viewgroup
我正在尝试为checkboxPreferences编写自定义视图.我有:
<CheckBoxPreference
android:key="@string/pref_acceptableads"
android:summaryOff="@string/pref_acceptableads_summary_off"
android:summaryOn="@string/pref_acceptableads_summary_on"
android:title="@string/pref_acceptableads_title"
android:widgetLayout="@layout/helpfulcheckbox_layout" />
Run Code Online (Sandbox Code Playgroud)
和:
<?xml version="1.0" encoding="UTF-8"?>
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</ViewGroup>
Run Code Online (Sandbox Code Playgroud)
问题在于我的viewgroup.我收到一个例外:
03-11 13:49:11.520:E/AndroidRuntime(12286):FATAL EXCEPTION:main 03-11 13:49:11.520:E/AndroidRuntime(12286):进程:org.adblockplus.android,PID:12286 03-11 13:49:11.520:E/AndroidRuntime(12286):android.view.InflateException:二进制XML文件行#2:错误膨胀类android.view.ViewGroup 03-11 13:49:11.520:E/AndroidRuntime(12286):在android.view.LayoutInflater.createView(LayoutInflater.java:620)03-11 13:49:11.520:E/AndroidRuntime(12286):at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)03-11 13 :49:11.520:E/AndroidRuntime(12286):at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)03-11 13:49:11.520:E/AndroidRuntime(12286):at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)03-11 13:49:11.520:E/AndroidRuntime(12286):at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)03-11 13: 49:11.520:E/AndroidRuntime(12286):在android.view.LayoutInflater.inflate(LayoutInfla)ter.java:469)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.view.LayoutInflater.inflate(LayoutInflater.java:397)03-11 13:49:11.520:E/AndroidRuntime( 12286):在android.view.LayoutInflater.inflate(LayoutInflater.java:353)03-11 13:49:11.520:E/AndroidRuntime(12286):at android.preference.Preference.onCreateView(Preference.java:489)03 -11 13:49:11.520:E/AndroidRuntime(12286):在android.preference.Preference.getView(Preference.java:460)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.preference .PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)03-11 13:49:11.520:E/AndroidRuntime(12286):at android.widget.AbsListView.obtainView(AbsListView.java:2255)03-11 13:49:11.520 :E/AndroidRuntime(12286):在android.widget.ListView.makeAndAddView(ListView.java:1790)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.widget.ListView.fillDown(ListView. java:691)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.widget.ListView.fillFromTop(ListView.java:752)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.widget.ListView.layoutChildren(ListView.java:1616)03-11 13:49:11.520:E/AndroidRuntime(12286):在android. widget.AbsListView.onLayout(AbsListView.java:2087)03-11 13:49:11.520:E/AndroidRuntime(12286):at android.view.View.layout(View.java:14841)03-11 13:49: 11.520:E/AndroidRuntime(12286):在android.view.ViewGroup.layout(ViewGroup.java:4631)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.widget.LinearLayout.setChildFrame(LinearLayout) .java:1671)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)03-11 13:49:11.520:E/AndroidRuntime(12286) ):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)03-11 13:49:11.520:E/AndroidRuntime(12286):在android.view.View.layout(View.java:14841)03- 11 …
我想创建一个带有2个TextView的自定义视图,可以从xml更改文本和文本外观.这个视图应该有两个状态 - normal和selected(TextViews样式应该对每个状态都不同).
我需要一些例子.
我想在现有布局的底部添加一个视图,而不管 Activity 布局文件中定义的 ViewGroup 的类型(线性、相对等...)
活动布局文件如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Ad"
android:layout_centerInParent="true"
android:onClick="showAdd"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是放置视图的代码:
ViewGroup rootView = (ViewGroup)((Activity) m_context).findViewById(android.R.id.content);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootView.addView(adView,lay);
Run Code Online (Sandbox Code Playgroud)
问题是 View 出现在屏幕中间而不是底部,正如预期的那样
我正在使用 InputMethodService、Keyboard 和 KeyboardView(我知道,它最近已被弃用)来创建自定义键盘。
override fun onCreateInputView(): View {
view = layoutInflater.inflate(
R.layout.keyboard_view_test,
null
)
keyboardView =
view.findViewById<KeyboardView>(R.id.keyboard)
keyboard = Keyboard(this, R.xml.keyboard_layout)
keyboardView.keyboard = keyboard
keyboardView.isPreviewEnabled = false
keyboardView.setOnKeyboardActionListener(this)
return keyboardView
}
Run Code Online (Sandbox Code Playgroud)
当使用 Keyboard_view_test.xml 中的 KeyboardView 作为根元素时,键盘工作得非常好。当我开始将它包装在 LinearLayout 或任何其他 ViewGroup 中而不更改任何其他内容时(请参阅keyboard_view_test.xml),我得到一个 IllegalStateException。我不明白,堆栈跟踪没有引用我的任何代码。我怎样才能让它发挥作用?
键盘视图测试.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGboardBackground">
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGboardBackground"
android:keyBackground="@drawable/key_background"
android:keyPreviewLayout="@layout/key_preview"
android:keyPreviewOffset="10dp"
android:keyTextColor="@color/colorGboardKeyText"
android:keyTextSize="22sp"
android:labelTextSize="22sp"
android:shadowRadius="0" />
Run Code Online (Sandbox Code Playgroud)
该错误似乎发生在 onCreateInputView() 返回后
错误堆栈:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on …Run Code Online (Sandbox Code Playgroud) android android-softkeyboard android-input-method android-viewgroup
我想显示一种颜色供用户预览。View我应该使用哪个?
我想要一个小方块,比如说,100dp。