使用文本视图制作像素矩阵



我的主要想法是创建一个像素矩阵并访问代码中的每个单元格以更改它的颜色 我有使用PixelArtist的文本视图的想法

我想在屏幕的一部分显示我的矩阵(35 * 10((例如屏幕尺寸的40%(并且应用程序仅在横向模式下运行.我的问题是应用程序仅显示一行。 来自模拟器的图像

主活动.xml代码是

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_mid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.60" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0" />
<LinearLayout
android:id="@+id/pic_frame_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/guideline_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_low">
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />
<include layout="@layout/paper_line" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我的纸线.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />
<TextView style="@style/Pixel" />


</LinearLayout>

和像素样式

<style name="Pixel">
<item name="android:layout_width">25dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">25dp</item>
<item name="android:layout_marginTop">1dp</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="background">@color/white</item>
</style>

谢谢

问题是my paperline.xml高度设置为match_parent将其更改为wrap_content

此外,如果您不打算在其中放置任何文本,那么您可能只使用View而不是TextView


此外,如果您不介意每个框更改大小,并且想要将所有框压缩到任何视图中,则可以更改以下内容:

使像素看起来像:

<style name="Pixel">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_marginTop">1dp</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="background">@color/white</item>
</style>

并使my paperline.xml顶部看起来像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">

通过将LinearLayout内视图的宽度或高度设置为0dp并提供权重,视图将根据权重动态计算宽度/高度。如果所有重量都相同,那么它将均匀分布。

最新更新