gpt4 book ai didi

android - 在 TableLayout Android 中的两个元素之间绘制自定义线

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:55 24 4
gpt4 key购买 nike

我有一个 Activity ,其中的事件按时间线组织。但它看起来很难看。

enter image description here

我想像这样设计一个更漂亮的时间线。 enter image description here

有没有像我的例子那样在元素之间画线的简单方法或库?

<ScrollView
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/text_data"
android:layout_above="@+id/button_trimite"
android:id="@+id/scroll_timeline"
android:layout_marginBottom="7dp"

>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/timelineTable"

>


</TableLayout>
</ScrollView>

这是我的 xml。但是我的 TableLayout 是动态生成的,因为我需要对我的事件进行排序。

for (final Event e : events) {
if(e.getDate().equals(dataComp)) {
//tablerow with event entry
final TableRow row = new TableRow(getActivity());

row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

if (indexForDrawable % 2 == 0)
row.setBackgroundResource(R.drawable.marcaj_event_albastru);
else
row.setBackgroundResource(R.drawable.marcaj_event_portocaliu);

TextView txtEvent = new TextView(getActivity());
txtEvent.setText(" "+ e.getHour() +"-"+e.getType()+"-"+e.getTitle());
txtEvent.setTextColor(Color.BLACK);
txtEvent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, trEvent);
txtEvent.setTypeface(Typeface.create(tf, Typeface.BOLD));

row.addView(txtEvent);
row.setClickable(true);
final String date = e.getDate(), hour = e.getHour(), title = e.getTitle(),
type = e.getType(), descriere = e.getDescriere();
final int finalResource = resource;
final int finalIndexForDrawable = indexForDrawable;
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
row.setBackground(getActivity().getResources().getDrawable(finalResource));
showPopup2(date, hour, type, title, descriere, row, finalIndexForDrawable);
}
});


timelineTable.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
indexForDrawable++;
}
else {
//tablerow with date
final TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView txtEvent = new TextView(getActivity());

//txtEvent.setText("\n"+ dataSplit1[0]+months.indexOf(dataSplit1 1 )); txtEvent.setText("\n"+ e.getDate().substring(0, 5)); txtEvent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, trDate); row.addView(txtEvent); timelineTable.addView(row, new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); dataComp = e.getDate();

            //tablerow with event entry
final TableRow row3 = new TableRow(getActivity());
row3.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

if (indexForDrawable % 2 == 0)
row3.setBackgroundResource(R.drawable.marcaj_event_albastru);
else
row3.setBackgroundResource(R.drawable.marcaj_event_portocaliu);
TextView txtEvent3 = new TextView(getActivity());

txtEvent3.setText(" "+ e.getHour() +"-"+e.getType()+"-"+e.getTitle());
txtEvent3.setTextColor(Color.BLACK);
txtEvent3.setTextSize(TypedValue.COMPLEX_UNIT_DIP, trEvent);
txtEvent3.setTypeface(Typeface.create(tf, Typeface.BOLD));
row3.addView(txtEvent3);
row3.setClickable(true);
final String date3 = e.getDate(), hour3 = e.getHour(), title3 = e.getTitle(),
type3 = e.getType(), descriere3 = e.getDescriere();

timelineTable.addView(row3, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
indexForDrawable++;
}

最佳答案

您可能必须创建自己的自定义适配器,但我使用数组适配器供您引用。还为 ListView 提供项目布局,希望您能相应地管理您的代码

list-items

items.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@android:color/black" />

<View
android:id="@+id/view1"
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_centerVertical="true"
android:layout_marginLeft="7dp"
android:background="@drawable/dot" />
</RelativeLayout>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:padding="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

dot.xml 这是一个可绘制对象

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<stroke
android:width="1dp"
android:color="@android:color/black" />

<solid android:color="@android:color/white" />

在 Activity 中你可以像这样使用适配器:

list.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.textView1, items));

希望这对您有所帮助!

关于android - 在 TableLayout Android 中的两个元素之间绘制自定义线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29625912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com