【问题标题】:Inflate a custom view from XML, then set her height and witdh programmatically从 XML 扩展自定义视图,然后以编程方式设置她的高度和宽度
【发布时间】:2014-08-01 18:14:38
【问题描述】:

我在自定义尺寸视图问题中迷失了几天。经过长时间的研究,我终于决定请求你的帮助......拜托! T_T

在我的应用程序中,我使用 XML calendrieradmin_day.xml 创建了一个自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/CalendrierAdmin_Day_Event_TableRow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!--  <TextView
        android:id="@+id/CalendrierAdmin_Day_Event_TextView_Name"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center" />-->
</TableRow>

目前,它是一个简单的 TableRow,但我以后需要添加一些其他内容。 (例如,注释的 TextView)。

我为使用它创建了三个类:RowEventRowEmptyRowRow 是抽象的,EventRowEmptyRow 继承于 Row。此外,Row 继承了 TableRow

我想在 TableLayout 中添加几个 EventRowEmptyRow,我需要动态调整它们的大小。

在 EmptyRow 和 EventRow 的构造方法中,我调用了一个 init() 方法。这是 EventRow 之一:

private void init(Context c, TableLayout root) {    
    root = (TableLayout)LayoutInflater.from(c).inflate(R.layout.calendrieradmin_day_event, root, true);

    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) root.getLayoutParams();

    lp.height = getHeightOfMinute() * getDuree();
    lp.width = getTheWidth();

    /*
    Some other stuff
    ...
    ...
    */

    this.setLayoutParams(lp);

    root.addView(this);

    setOnClickListener(this);
    setOnLongClickListener(this);
}

当然,它不能作为例外工作。 EventRow 未显示。

root 是我的 TableLayout 我传入构造函数。 getHeightOfMinute()getDuree() 工作正常并给我正确的尺寸。我曾经使用像 inflate(R.layout.calendrieradmin_day_event, null, false) 这样的 inflate() 方法,它工作得很好,但是 Eclipse 给了我一个警告,告诉我我不应该将 null 作为 RootView 参数传递。尽管一切正常,但我开始寻找另一种解决方案,这就是我目前为止的目标......

我使用 setMinimumHeight()setMinimumWidth() 方法来调整我的 EventRowEmptyRow。它有效,但我认为有更好的方法来做到这一点。我尝试了很多解决方案,但我搞砸了!我认为关于 inflate() 和/或 LayoutParams 的功能我还没有理解。

你能帮我吗?提前致谢 !

如果您需要更多源代码,请告诉我。

PS:抱歉英语不好,我是法国人。

【问题讨论】:

  • Viliktùr 你有没有得到这个?我面临着类似的问题
  • 是的,但我改变了很多东西...... :-)
  • 好的,我昨天设法让它自己工作,不得不同时覆盖 onLayout 和 onMeasure。
  • 我设法通过使用一些 LinearLayout 和 weight 属性使其工作。你想让我说得更直白吗?如果你能解释一下你自己的方法,我会很高兴,我相信它总有一天会有用。

标签: android xml custom-view android-inflate layoutparams


【解决方案1】:

我终于成功了,但是很多东西都变了。

首先,我删除了 TableLayoutTableRow。现在它只是一些 LinearLayout

calendrieradmin_day.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/CalendrierAdmin_Day_Event_LinearLayout_Container"
  android:layout_width="fill_parent"
  android:layout_height="0dp"
  android:orientation="horizontal" >

  <TextView
    android:id="@+id/CalendrierAdmin_Day_Event_TextView_Name"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="left"
    android:background="@drawable/background_event_full"
    android:gravity="left" />

</LinearLayout>

这就是 init() 方法:

private void init(Context c, LinearLayout root) {   
    LinearLayout container = (LinearLayout) LayoutInflater.from(c).inflate(R.layout.calendrieradmin_day_event, root, false);
    TextView name = (TextView) container.findViewById(R.id.CalendrierAdmin_Day_Event_TextView_Name);


    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 0, duree);        
    container.setLayoutParams(lp);

    name.setLayoutParams(lptv);


    name.setText(e.getNom());

    root.addView(container);

    name.setOnClickListener(this);
    name.setOnLongClickListener(this);
}

注意LayoutParams lp(它是一个LinearLayout.LayoutParams)。 duree 是我的活动的持续时间,以分钟为单位。

我的 EventRow 打算在另一个 LinearLayoutllEvents 中。它包含一天的所有事件,因此 1440 分钟 (60 x 24)。结果 llEvents 的 weightSum 为 1440。

我删除了 EmptyRowRow 类。我只是将一些空的 LinearLayout 添加到 llEvents 以作为空闲时间安排,其中持续时间为 weight

在 LayoutParams 中预先设置“0”高度非常重要

希望它会帮助某人 ;-) !

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 2012-01-23
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2012-05-24
    相关资源
    最近更新 更多