我知道在XML中,视图很昂贵,理想情况下你应该尽可能少地努力。但在我的情况下,我正在以这样的方式设计我的布局,我被迫有很多视图,接近80.Eclipse给了我一个警告,所以我删除了一些视图,但我只能做得太多,因为我的实现需要最少,48个视图,并且不包括一些额外的东西。所以我的问题在于我是否应该找到一种更简单的方法,或者它是否真的如此。真正应该最大的实际数字是多少?
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/top_bar_date_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="July 1, 2012"
android:background="#696969"/>
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/day_view_floaty_holder"
android:background="#000000">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="12\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal"
android:layout_marginBottom="1dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="1\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="2\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="3\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="4\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="5\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="6\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="7\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="8\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="9\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="10\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="11\nAM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="12\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="1\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="2\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="3\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="4\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="5\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="6\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="7\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="8\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="9\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="10\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#ffffff"
android:layout_marginBottom="1dp" >
<TextView
android:layout_width="20dp"
android:layout_height="60dp"
android:text="11\nPM"
android:background="#bebebe"
android:textSize="10dp"
android:gravity="center_horizontal" />
</LinearLayout>
</GridLayout>
<!-- THIS GRIDLAYOUT IS WHERE THE EVENTS WILL BE PLACED-->
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_0"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_1"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_2"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_3"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_4"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_5"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_6"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_7"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_8"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_9"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_10"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_11"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_12"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_13"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_14"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_15"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_16"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_17"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_18"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_19"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_20"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_21"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_22"
android:orientation="horizontal" ></LinearLayout>
<LinearLayout
android:layout_height="61dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:id="@+id/day_view_23"
android:orientation="horizontal" ></LinearLayout>
</GridLayout>
</RelativeLayout>
</ScrollView>
我应该解释代码转换为背后的逻辑。我基本上需要一个24行的GridLayout,以及在它上面浮动的另外24行的另一个GridLayout。我这样做是为了将我需要浮动的东西分开到第一个GridLayout之上。它假设是日历应用程序的日视图。
答案 0 :(得分:1)
根据您的xml文档,您可以使用ListView和ListAdapter轻松解决此问题。
您可以将该xml文档缩小为一个包含“top_bar_date_display”TextView和ListView的主xml文件。其次,您将拥有一个包含列表行项的xml布局文件。然后ListAdapter将设置行的详细信息。然后,您实现一个列表适配器,其中包含一组自定义对象,表示您需要的每一行。列表适配器根据数组中的哪个位置设置getView方法中的文本。
列表行可能如下所示:
<LinearLayout
android:id="@+id/rowItem"
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:layout_width="20dp"
android:layout_height="match_parent"
android:background="#bebebe"
android:layout_marginBottom="1dp"
android:gravity="center_horizontal"
android:text="[Replaced by ListAdapter]"
android:textSize="10dp" />
<LinearLayout
android:id="@+id/event1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp" />
</LinearLayout>
编辑:
如果listview没有分隔符,我们将行视图的子项设置为样式,以便创建一个假分隔符,然后列表适配器可以为第1行设置'event1'的宽度,并为'event1使用相同的宽度由于'event1'在两行之间,你会选择row1中的'event1'显示没有(假的)分隔符,因为它们具有相同的背景,它们看起来好像是一个对象,你会有尽管如此,为压榨国家做一些愚蠢的事情。
ListAdapter还可以使用rowItem.addView(new LinearLayout)动态创建要添加的新事件。只需要跟踪所有事件,它们跨越的行和任何碰撞。
答案 1 :(得分:1)
正如我所说,我会使用ListView
来显示数据以避免产生如此多的观看次数。首先,我将创建一个类来表示您在列表中显示的用户事件:
private static class Event {
public int startHour;
public int hours = 1;
public String name;
public String date;
public boolean isEmptyEvent = true;
public Event(int startHour, int hours, String name, String date,
boolean isEmptyEvent) {
this.startHour = startHour; 0 to 23 (the method getStartHour makes this pretty)
this.hours = hours; // how many this event requires(default 1)
this.name = name;
this.date = date;
this.isEmptyEvent = isEmptyEvent;
}
public String getStartHour() {
// should optimize this method so you don't end up creating String
// after String(char array?)!
if (startHour < 10) {
return "0" + startHour + ":00";
}
return String.valueOf(startHour) + ":00";
}
}
然后我将解析我在Events
对象中的数据(我不知道你在数据库中保存了什么,所以这只是猜测):
public SparseArray<Event> getData(String date) {
// here you get the data from the database and create Events object for
// those dates
SparseArray<Event> data = new SparseArray<Event>(); // for efficiency
// add some random events
data.put(3, new Event(3, 1, "Breackfast", date, false));// one event at 3 taking 1 hour
data.put(14, new Event(14, 3, "Lunch", date, false));// one event at 14 taking 3 hour
data.put(20, new Event(20, 2, "Dinner", date, false));// one event at 20 taking 2 hour
return data;
}
然后在适配器中,我将用空Events
个对象填充剩余的空条目(用户尚未设置任何内容),并将其显示给用户。由于代码有点大,我不会在这里发布,而是你可以在这里找到完整的活动+布局https://gist.github.com/3370107