如何根据时间段动态显示自定义视图?
How to show a custom view dynamically based on time slots?
我正在创建一个时间 table 应用程序,我想在其中显示创建的事件。为此,我创建了布局,在其中我创建了一个滚动视图,其中包含一个相对布局。我确保这些布局的高度等于一天中的分钟数。这将使 1 小时 = 60 分钟 = 60 dp,这使得测量事件的高度更容易。
对于活动,我创建了一个自定义视图,可以显示活动的开始和结束时间,以及活动的标题。我要添加到具有 layout_marginTop
属性 的相对布局的事件,其值等于事件的开始时间,以分钟为单位从一天开始。
例如,如果我想在上午 01:30 添加一个时长为 1 小时的事件,我会添加事件视图,距列顶部 90 dp,高度为 60 dp。
我知道我必须做什么,但无法在代码中实现它。有谁能帮忙吗
现在布局如下:
活动视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Start Time"
android:id="@+id/textView2"
android:layout_gravity="right"
android:layout_marginStart="61dp"
android:layout_alignTop="@+id/textView"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="End Time"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView2"
android:layout_marginStart="87dp"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="10dp" />
</RelativeLayout>
在关注您的回答后,我收到了两个这样的事件:
如何查看和获取不同时间的事件?
而且事件的高度没有变化..
我假设你的时间布局看起来像这样。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView>
<RelativeLayout android:id="@+id/mytimetable">
<!-- Your time blocks -->
</RelativeLayout>
</ScrollView>
并且您想在运行时使用自定义高度和边距将事件的自定义视图添加到此 RelativeLayout 中。你可以通过改变它的 LayoutParams
来做到这一点
//Getting your relative layout where you are going to place your events
ViewGroup dayplanView = (ViewGroup) layout.findViewById(R.id.mytimetable);
//Inflating your eventview but not adding it yet. Note that event_layout is your event layout resource.
View eventView = inflater.inflate(R.layout.event_layout, dayplanView, false);
//Getting its current layoutparams in order to change them
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
//Setting its height (in pixels) (1h 30min)
layoutParams.height = dpToPixels(90);
//Setting topmargin (in pixels) (1.30am)
layoutParams.topMargin = dpToPixels(90);
//Adding layoutparams back to the view
eventView.setLayoutParams(layoutParams);
//Adding your eventview to the relativeview
dayplanView.addView(eventView);
特此我创建了 dpToPixels 方法来将 dp 转换为像素。它是 TypedValue.applyDimension 的简化版本。
它看起来像这样。
private int dpToPixels(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density);
}
希望对您有所帮助。祝你的应用程序好运。
我正在创建一个时间 table 应用程序,我想在其中显示创建的事件。为此,我创建了布局,在其中我创建了一个滚动视图,其中包含一个相对布局。我确保这些布局的高度等于一天中的分钟数。这将使 1 小时 = 60 分钟 = 60 dp,这使得测量事件的高度更容易。
对于活动,我创建了一个自定义视图,可以显示活动的开始和结束时间,以及活动的标题。我要添加到具有 layout_marginTop
属性 的相对布局的事件,其值等于事件的开始时间,以分钟为单位从一天开始。
例如,如果我想在上午 01:30 添加一个时长为 1 小时的事件,我会添加事件视图,距列顶部 90 dp,高度为 60 dp。
我知道我必须做什么,但无法在代码中实现它。有谁能帮忙吗
现在布局如下:
活动视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Start Time"
android:id="@+id/textView2"
android:layout_gravity="right"
android:layout_marginStart="61dp"
android:layout_alignTop="@+id/textView"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="End Time"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView2"
android:layout_marginStart="87dp"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="10dp" />
</RelativeLayout>
在关注您的回答后,我收到了两个这样的事件:
如何查看和获取不同时间的事件? 而且事件的高度没有变化..
我假设你的时间布局看起来像这样。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView>
<RelativeLayout android:id="@+id/mytimetable">
<!-- Your time blocks -->
</RelativeLayout>
</ScrollView>
并且您想在运行时使用自定义高度和边距将事件的自定义视图添加到此 RelativeLayout 中。你可以通过改变它的 LayoutParams
来做到这一点//Getting your relative layout where you are going to place your events
ViewGroup dayplanView = (ViewGroup) layout.findViewById(R.id.mytimetable);
//Inflating your eventview but not adding it yet. Note that event_layout is your event layout resource.
View eventView = inflater.inflate(R.layout.event_layout, dayplanView, false);
//Getting its current layoutparams in order to change them
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
//Setting its height (in pixels) (1h 30min)
layoutParams.height = dpToPixels(90);
//Setting topmargin (in pixels) (1.30am)
layoutParams.topMargin = dpToPixels(90);
//Adding layoutparams back to the view
eventView.setLayoutParams(layoutParams);
//Adding your eventview to the relativeview
dayplanView.addView(eventView);
特此我创建了 dpToPixels 方法来将 dp 转换为像素。它是 TypedValue.applyDimension 的简化版本。 它看起来像这样。
private int dpToPixels(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density);
}
希望对您有所帮助。祝你的应用程序好运。