Android: 如何自定义textview或listview
Android: how to customize a textview or a listview
我正在尝试创建类似的东西。
我不确定这是文本视图还是列表视图。我尝试使用 TextView 并查看发生了什么,但我遇到了以下问题:
- 我的标题在 textview 之外(这是一个不同的 textview),我为我的标题创建一个不同的 textview 的原因是我想要一个不同字体的标题。
- 我也不知道如何在项目符号点之间创建那条线。
这是我目前拥有的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Interns"
android:verticalScrollbarPosition="defaultPosition"
android:scrollbars="vertical"
android:orientation="vertical">
<TextView
android:id="@+id/internsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textAbout"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Title"
android:textAlignment="center"
android:textColor="@color/colorBlack"
android:textSize="24sp" />
<TextView
android:id="@+id/textInterns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/rounded_corner"
android:gravity="center"
android:lineSpacingExtra="6sp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="List of items"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
<Button
android:id="@+id/button_website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/visti_site"
android:textSize="12sp"
android:drawableLeft="@drawable/ic_language_black_24dp"
android:drawablePadding="3dp"
android:gravity="left|center_vertical"
android:background="@drawable/rounded_corner"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginBottom="10dp"
/>
<Button
android:id="@+id/button_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/email_us_text"
android:textSize="12sp"
android:drawableLeft="@drawable/ic_email_green_24dp"
android:drawablePadding="3dp"
android:gravity="left|center_vertical"
android:background="@drawable/rounded_corner"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</ScrollView>
我怀疑你图片中的每张卡片都有一个 TextView 作为标题,然后是一个 ListView 或 RecyclerView 作为标题下方的项目符号。
各个要点可以用这样的布局来制作:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/circle"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="4dp"
android:layout_marginStart="8dp"
android:src="@drawable/circle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/line"
android:layout_width="3dp"
android:layout_height="0dp"
android:layout_marginTop="2dp"
android:scaleType="fitCenter"
app:layout_constraintTop_toBottomOf="@+id/circle"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/line"
app:layout_constraintStart_toStartOf="@+id/circle"
app:layout_constraintEnd_toEndOf="@+id/circle"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/circle"
app:layout_constraintEnd_toEndOf="parent"
tools:text="@string/lorem_ipsum"/>
</android.support.constraint.ConstraintLayout>
circle
和 line
可绘制对象是简单的形状:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#caf"/>
</shape>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#caf"/>
</shape>
拼图的最后一块是拼图,以便在最后一个项目符号点时不可见该行。一种简单的方法是根据您是否绑定到列表中的最后一项来设置其可见性。如果您使用的是 RecyclerView,它可能看起来像这样:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
...
if (position == getItemCount() - 1) {
holder.line.setVisibility(View.INVISIBLE);
} else {
holder.line.setVisibility(View.VISIBLE);
}
}
我正在尝试创建类似的东西。
我不确定这是文本视图还是列表视图。我尝试使用 TextView 并查看发生了什么,但我遇到了以下问题:
- 我的标题在 textview 之外(这是一个不同的 textview),我为我的标题创建一个不同的 textview 的原因是我想要一个不同字体的标题。
- 我也不知道如何在项目符号点之间创建那条线。
这是我目前拥有的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Interns"
android:verticalScrollbarPosition="defaultPosition"
android:scrollbars="vertical"
android:orientation="vertical">
<TextView
android:id="@+id/internsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textAbout"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Title"
android:textAlignment="center"
android:textColor="@color/colorBlack"
android:textSize="24sp" />
<TextView
android:id="@+id/textInterns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/rounded_corner"
android:gravity="center"
android:lineSpacingExtra="6sp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="List of items"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
<Button
android:id="@+id/button_website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/visti_site"
android:textSize="12sp"
android:drawableLeft="@drawable/ic_language_black_24dp"
android:drawablePadding="3dp"
android:gravity="left|center_vertical"
android:background="@drawable/rounded_corner"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginBottom="10dp"
/>
<Button
android:id="@+id/button_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/email_us_text"
android:textSize="12sp"
android:drawableLeft="@drawable/ic_email_green_24dp"
android:drawablePadding="3dp"
android:gravity="left|center_vertical"
android:background="@drawable/rounded_corner"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</ScrollView>
我怀疑你图片中的每张卡片都有一个 TextView 作为标题,然后是一个 ListView 或 RecyclerView 作为标题下方的项目符号。
各个要点可以用这样的布局来制作:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/circle"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="4dp"
android:layout_marginStart="8dp"
android:src="@drawable/circle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/line"
android:layout_width="3dp"
android:layout_height="0dp"
android:layout_marginTop="2dp"
android:scaleType="fitCenter"
app:layout_constraintTop_toBottomOf="@+id/circle"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/line"
app:layout_constraintStart_toStartOf="@+id/circle"
app:layout_constraintEnd_toEndOf="@+id/circle"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/circle"
app:layout_constraintEnd_toEndOf="parent"
tools:text="@string/lorem_ipsum"/>
</android.support.constraint.ConstraintLayout>
circle
和 line
可绘制对象是简单的形状:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#caf"/>
</shape>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#caf"/>
</shape>
拼图的最后一块是拼图,以便在最后一个项目符号点时不可见该行。一种简单的方法是根据您是否绑定到列表中的最后一项来设置其可见性。如果您使用的是 RecyclerView,它可能看起来像这样:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
...
if (position == getItemCount() - 1) {
holder.line.setVisibility(View.INVISIBLE);
} else {
holder.line.setVisibility(View.VISIBLE);
}
}