相关疑难解决方法(0)

带孩子的自定义Android控件

我试图创建一个定制的Android控制包含一个LinearLayout中.您可以将其视为具有花式边框,背景,左侧图像的扩展LinearLayout ...

我可以用XML完成所有工作(效果很好),但由于我的应用程序中有很多次出现,因此很难维护.我觉得这样的东西会更好:

/* Main.xml */
<MyFancyLayout>
    <TextView />   /* what goes inside my control's linear layout */
</MyfancyLayout>
Run Code Online (Sandbox Code Playgroud)

你会怎么做?我想避免在onMeasure/onLayout方法上重写整个线性布局.这就是我现在所拥有的:

/* MyFancyLayout.xml */
<TableLayout>
    <ImageView />
    <LinearLayout id="container" />   /* where I want the real content to go */
</TableLayout>    
Run Code Online (Sandbox Code Playgroud)

/* MyFancyLayout.java */
public class MyFancyLayout extends LinearLayout
{
    public MyFancyLayout(Context context) {
        super(context);
        View.inflate(context, R.layout.my_fancy_layout, this);
    }
}
Run Code Online (Sandbox Code Playgroud)

您如何在正确的位置(id =容器)插入用户指定的内容(main.xml中的T​​extView)?

干杯!

罗曼

-----编辑-------

仍然没有运气,所以我改变了我的设计,使用更简单的布局,并决定使用一些重复的XML.仍然非常感兴趣的人知道如何做到这一点!

android border android-linearlayout

12
推荐指数
3
解决办法
2993
查看次数

android中的图例和字段集

我想知道在android中是否可以执行这样的表单:

在此输入图像描述

此刻,我做了一个形状来生成边框(我使用这个形状作为我的线性布局的背景,但我不知道如何处理文本).

提前致谢.

android fieldset legend

7
推荐指数
1
解决办法
8754
查看次数

布局没有在android自定义组件中膨胀

我在自定义视图(从a派生LinearLayout)中得到一个空指针异常,因为它找不到它的子视图.这是代码:

public class MyView extends LinearLayout
{
    public MyView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    private TextView mText;

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        mText = (TextView) findViewById(R.id.text);

        if (isInEditMode())
        {
            mText.setText("Some example text.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是布局(my_view.xml):

<?xml version="1.0" encoding="utf-8"?>
<com.example.views.MyView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:ellipsize="end"
        android:maxLines="4"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Some text" />

</com.example.views.MyView>
Run Code Online (Sandbox Code Playgroud)

这是我把它放在XML文件中的方式: …

android custom-component android-custom-view layout-inflater

5
推荐指数
1
解决办法
2337
查看次数

Android自定义视图组委托addView

我希望ViewGroup在我的情况下实现自定义,FrameLayout但我希望从xml添加的所有子视图不是直接添加到此视图中,而是FrameLayout包含在此自定义中ViewGroup.让我举例说明一下.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
    <FrameLayout
        android:id="@+id/frame_layout_child_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <FrameLayout
        android:id="@+id/frame_layout_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</merge>
Run Code Online (Sandbox Code Playgroud)

我想重定向将所有子视图添加到FrameLayoutid frame_layout_child_container.

所以当然我推翻方法addView()是这样

  @Override
    public void addView(View child) {
        this.mFrameLayoutChildViewsContainer.addView(child);
    }
Run Code Online (Sandbox Code Playgroud)

但肯定这不起作用,因为此时mFrameLayoutChildViewsContainer不会添加到根自定义视图.

我的想法总是在这个容器的顶部保留一些视图,frame_layout_top并且所有添加到自定义组件中的子视图都应该去frame_layout_child_container

使用自定义视图的示例

   <CustomFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </CustomFrameLayout>
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下TextView应该添加到frame_layout_child_container

是否可以委托ViewGroup像我描述的那样将所有视图添加到子项中.

我有其他的想法,比如bringToFront()每次添加视图时使用方法以保持正确的z轴顺序,或者例如添加视图时,将其保存到数组中,然后在充气自定义视图后将所有视图添加到此子项FrameLayout

建议在这种情况下做什么,以便在每次添加新视图时都不会通过重新填充所有布局来达到性能,如果可以以其他方式实现的话.

performance android android-custom-view android-layout android-view

4
推荐指数
1
解决办法
2424
查看次数