xml 相关问题

可扩展标记语言(XML)是一种灵活的结构化文档格式,用于定义人类和机器可读的编码规则。

如何使用 Pandoc 将 css 样式传递到 html

我希望在 Windows 10 上使用 pandoc 3.0 将 docbook xml 输入转换为 html。 如何让 pandoc 识别文本颜色或其他文本样式?我的 docbooc xml 输入是 我希望在 Windows 10 上使用 pandoc 3.0 将 docbook xml 输入转换为 html。 如何让 pandoc 识别文本颜色或其他文本样式?我的 docbooc xml 输入是 <section id="body1"> <title>Section with a title</title> <para> In the year <emphasis role="red">1878</emphasis> I took my degree of Doctor of Medicine of the University of London, and proceeded to Netley to go through the course prescribed for surgeons in the army. Having completed my studies there, I was duly attached to the Fifth Northumberland Fusiliers as Assistant Surgeon. The regiment was stationed in India at the time, and before I could join it, the second Afghan war had broken out. </para> </section> 我希望 1878 以红色显示,类似于 <span style=xxx>1878</span>,其中提供的 CSS 会将 xxx 识别为 font-color: red。 我正在使用 pandoc --standalone --embed-resources --css=my.css -f docbook \ -t html -o foo.html` FWIW,我使用的是工作笔记本电脑,因此可能无法安装额外的 XML/XSL 软件。 我花了 2 天谷歌搜索这个,我已经阅读了 Sagehill 自定义内联内容,但我无法弄清楚如何实际应用自定义内联样式。另外,如果可能的话,我希望通过在 docbook XML 文档中包含 XSL 魔法来实现此目的。 在我所有的 SageHill 文档搜索中 这里 所以问题73652391可能最接近我想要的。但除非我遗漏了一些东西,否则 SageHill 的东西似乎需要 xsltproc 才能工作,即 Pandoc 不能做 xsltproc 可以做的事情。我想像这样的问题47474222也会起作用,但我不知道该怎么做。 我已经阅读并重新阅读了几十个问题,但它们似乎都指的是 Markdown 输入,这对我来说不是一个选择。我正在转换旧 XML 文档库来生成 HTML 并执行许多其他任务,因此 Markdown 输入等对我来说不是一个选择。 这里的主要问题是 pandoc 保留文档的结构,但不保留格式细节。文本颜色被视为格式细节。这种信息在第一步读入文档时就已经丢失,因此在其余步骤中无法保留它。信息丢失是由于 pandoc 的限制之一所致,请参阅此处。 一种选择可能是找到这些彩色文本的共同点,然后应用上述问答中的方法。例如,如果所有仅包含数字的强调元素都被着色为红色,那么像这样的 Lua 过滤器应该这样做: function Emph (em) if pandoc.utils.stringify(em):match '^[0-9]*$' then return pandoc.Span(em, {style="font-color:red"}) end end

回答 1 投票 0

解析 youtube rss feed

有没有像 ruby 一样快速干净地读取这些媒体键的示例? 我已经在使用 ruby rss 并且正在阅读标题、链接,但这些带分号的媒体键我无法阅读。我应该使用...

回答 2 投票 0

Android - 无法按预期设置按钮边框

我前几天刚开始学习Android。今天当我玩 Button 时,我注意到我无法按预期设置边框。 下面的 xml 文件: Activity_main.xml 中的按钮部分:...

回答 4 投票 0

如果字符串以另一个单词 regex perl 开头,则忽略该字符串

我正在阅读一个 XML 文件,其中我正在替换某些单词。我不想替换 XML 元素或属性中的单词。我使用消极的向后看和向前看,我已经完成了 90%。

回答 1 投票 0

访问下一个兄弟的文本

这是 jenkins xml 文件的一部分。 我想用 xpath 提取project_name 的defaultValue。 在本例中,该值是 *****。 ...

回答 2 投票 0

lxml(xml python 解析器)转到下一个元素

有人知道如何跳转到xml中的下一个元素吗? IE 文字编号2 文字...

回答 1 投票 0

为什么按钮上不显示边框?

我正在 Android Studio 中构建一个应用程序,我试图在按钮周围添加边框。 具体来说,这是一个我试图将其添加到的按钮: 我正在 Android Studio 中构建一个应用程序,并尝试在按钮周围添加边框。 具体来说,这是一个我试图将其添加到的按钮: <Button android:id="@+id/activitiesButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/activities" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/healthBeautyButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/mbCardButton" tools:ignore="VisualLintButtonSize" /> 这是整个文件 actvity_home.xml 的代码: <?xml version="1.0" encoding="utf-8"?> <ScrollView 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" android:fillViewport="true" style="@style/ButtonStyle" tools:ignore="SpeakableTextPresentCheck"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" tools:context=".HomeActivity" tools:ignore="Overdraw" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/virtual_card" android:textColor="#FEBE0F" android:textSize="27sp" android:textStyle="bold" android:gravity="center_horizontal" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="20sp" app:autoSizeMaxTextSize="25sp" app:autoSizeStepGranularity="1sp" android:paddingTop="10dp" android:paddingBottom="10dp" app:layout_constraintBottom_toTopOf="@+id/mbCardButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="TextSizeCheck" /> <ImageView android:id="@+id/mbCardButton" android:layout_width="0dp" android:layout_height="0dp" android:contentDescription="@string/mane_benefits_logo" app:layout_constraintBottom_toTopOf="@+id/activitiesButton" app:layout_constraintDimensionRatio="W,2:1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" app:srcCompat="@drawable/mb_card" tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,ImageContrastCheck" /> <Button android:id="@+id/activitiesButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/activities" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/healthBeautyButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/mbCardButton" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/healthBeautyButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/health_beauty" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/restaurantsButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/activitiesButton" tools:ignore="TextSizeCheck,VisualLintButtonSize" /> <Button android:id="@+id/restaurantsButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/restaurants" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/retailButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/healthBeautyButton" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/retailButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/retail" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/servicesButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/restaurantsButton" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/servicesButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/services" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/sevenPointsButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/retailButton" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/sevenPointsButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" style="@style/ButtonStyle" android:text="@string/_7_points" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/mbLogo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/servicesButton" tools:ignore="VisualLintButtonSize" /> <ImageView android:id="@+id/ffcuLogo" android:layout_width="64dp" android:layout_height="74dp" android:layout_marginStart="0dp" android:layout_marginTop="0dp" android:layout_marginEnd="0dp" android:layout_marginBottom="20dp" android:contentDescription="@string/ffcu_logo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/mbLogo" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton" app:srcCompat="@drawable/ffcu_logo" /> <ImageView android:id="@+id/mbLogo" android:layout_width="64dp" android:layout_height="74dp" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:layout_marginBottom="20dp" android:contentDescription="@string/mane_benefits_logo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/fbButton" app:layout_constraintStart_toEndOf="@+id/ffcuLogo" app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton" app:srcCompat="@drawable/mb_logo_new" /> <ImageButton android:id="@+id/fbButton" android:layout_width="51dp" android:layout_height="76dp" android:layout_marginStart="0dp" android:layout_marginTop="0dp" android:layout_marginEnd="0dp" android:layout_marginBottom="20dp" android:contentDescription="@string/mb_fb_logo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/instaButton" app:layout_constraintStart_toEndOf="@+id/mbLogo" app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton" app:srcCompat="@drawable/mb_fb" tools:ignore="SpeakableTextPresentCheck" /> <ImageButton android:id="@+id/instaButton" android:layout_width="64dp" android:layout_height="68dp" android:layout_marginStart="0dp" android:layout_marginTop="0dp" android:layout_marginEnd="20dp" android:layout_marginBottom="20dp" android:contentDescription="@string/mb_insta_logo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/fbButton" app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton" app:srcCompat="@drawable/mb_insta" tools:ignore="SpeakableTextPresentCheck,ImageContrastCheck" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> 这是 styles.xml: <resources> <style name="ButtonStyle" parent="Widget.AppCompat.Button"> <item name="android:background">@drawable/border</item> <!-- Reference the correct drawable name --> <item name="android:textColor">#FEBE0F</item> <!-- Text color --> <item name="android:textSize">18sp</item> <!-- Example text size --> <item name="android:padding">10dp</item> <!-- Example padding --> </style> </resources> 这是 border.xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#632994" /> <!-- Background color --> <stroke android:width="5dp" android:color="#FEBE0F" /> <!-- Border color and width --> <corners android:radius="5dp" /> <!-- Rounded corners --> </shape> 在设计视图中,当我在 border.xml 中时,我看到边框,它看起来像这样: 但在我的 home_activity.xml 中它看起来像这样: 我尝试过让按钮的背景像边框一样 <Button android:id="@+id/sevenPointsButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:text="@string/_7_points" android:background="@drawable/border" android:textColor="#FEBE0F" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/mbLogo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/servicesButton" tools:ignore="VisualLintButtonSize" /> 我什至将它放在整个视图周围,看看它是否有效,正如您从上面的图片中看到的那样。 除了将这些按钮设置为转到 HomeActivity.java 中的不同视图之外,没有其他任何东西会影响这些按钮: public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); Button transitionButton = findViewById(R.id.activitiesButton); transitionButton.setOnClickListener(v -> { Intent intent = new Intent(HomeActivity.this,ActivitiesActivity.class); startActivity(intent); }); transitionButton = findViewById(R.id.healthBeautyButton); transitionButton.setOnClickListener(v -> { Intent intent = new Intent(HomeActivity.this,HealthBeautyActivity.class); startActivity(intent); }); transitionButton = findViewById(R.id.restaurantsButton); transitionButton.setOnClickListener(v -> { Intent intent = new Intent(HomeActivity.this,RestaurantsActivity.class); startActivity(intent); }); transitionButton = findViewById(R.id.retailButton); transitionButton.setOnClickListener(v -> { Intent intent = new Intent(HomeActivity.this,RetailActivity.class); startActivity(intent); }); transitionButton = findViewById(R.id.servicesButton); transitionButton.setOnClickListener(v -> { Intent intent = new Intent(HomeActivity.this,ServicesActivity.class); startActivity(intent); }); transitionButton = findViewById(R.id.sevenPointsButton); transitionButton.setOnClickListener(v -> { Intent intent = new Intent(HomeActivity.this,SevenPointsActivity.class); startActivity(intent); }); } } 尝试使用不同的按钮类型,因为这些按钮很难自定义 <androidx.appcompat.widget.AppCompatButton android:id="@+id/sevenPointsButton" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:text="@string/_7_points" android:background="@drawable/border" android:textColor="#FEBE0F" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="5sp" app:autoSizeMaxTextSize="30sp" app:autoSizeStepGranularity="1sp" app:layout_constraintBottom_toTopOf="@+id/mbLogo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/servicesButton" tools:ignore="VisualLintButtonSize" /> 试试这个

回答 1 投票 0

Libxml2:输出带有属性和内容的XML元素

我正在使用 libxml2 XMLTextWriter API(此处提供了官方示例)来输出 XML,但找不到任何示例或了解如何生成具有属性和内容的元素,...

回答 1 投票 0

设置内联 SVG 样式时是否需要 CDATA?

我读过有关此问题的各种内容,所以我希望能对其进行明确的排序。 如果我有内嵌在 HTML 文档中的 SVG 标记,并且我使用 CSS 对其进行样式设置,我是否需要包装该 CSS...

回答 1 投票 0

希望 recyclerview 在创建活动时从下到上开始,并在添加新项目时再次在底部滚动

我的 Android 应用程序中有一个名为 ChatActivity 的活动。这是 xml 布局: 我的 Android 应用程序中有一个名为 ChatActivity 的活动。这是 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:windowSoftInputMode="adjustResize" tools:context=".ui.home.ui.chat.ChatActivity"> <com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:navigationIcon="?attr/homeAsUpIndicator" app:navigationIconTint="@color/black" app:title="@string/chat" app:titleCentered="true" > <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/ivProfile" android:layout_width="@dimen/_30sdp" android:layout_height="@dimen/_30sdp" android:src="@drawable/ic_dummy_user" android:layout_marginHorizontal="@dimen/_10sdp" android:layout_gravity="end" /> </com.google.android.material.appbar.MaterialToolbar> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvMessages" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/toolbar" android:paddingBottom="@dimen/_95sdp" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintVertical_bias="1.0" tools:listitem="@layout/item_message_sent" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/tiMessage" style="@style/textInputLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:maxHeight="@dimen/_200sdp" android:layout_marginLeft="@dimen/_5sdp" android:layout_marginRight="@dimen/_35sdp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" app:errorEnabled="false" android:layout_marginVertical="@dimen/_5sdp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" > <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:maxHeight="@dimen/_200sdp" android:autofillHints="name" android:hint="@string/message" android:inputType="textMultiLine" /> </com.google.android.material.textfield.TextInputLayout> <ImageView android:id="@+id/ivSend" android:layout_width="@dimen/_25sdp" android:layout_height="@dimen/_25sdp" android:src="@drawable/ic_send_message" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="@dimen/_12sdp" app:layout_constraintLeft_toRightOf="@id/tiMessage" app:tint="@color/blue" /> </androidx.constraintlayout.widget.ConstraintLayout> 当我打开 textInput 字段时,布局不适应它,它仅显示要打开的键盘,但是当我在键盘中键入时,它确实会在文本输入布局中键入,但在键入时用户无法看到文本输入布局。关闭键盘 + 回收视图开始从上到下滚动,但我希望它像聊天一样,从下到上。在添加的新消息上,它确实滚动到底部,但在现有列表的最后一项上,而不是在新项目上。 我尝试过使用 android:windowSoftInputMode="adjustResize" 作为键盘和 private fun scrollToBottom() { binding.rvMessages.scrollToPosition(adapter.itemCount -1) } 用于回收视图。 欢迎任何优化代码/改进的建议。我的活动代码是: @AndroidEntryPoint class ChatActivity : AppCompatActivity() { private lateinit var binding: ActivityChatBinding private lateinit var database: DatabaseReference private lateinit var adapter: ChatAdapter private var currentUserId: Int = 0 private var otherUserId: Int = 0 private lateinit var conversationId: String private var convoId = "" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() binding = ActivityChatBinding.inflate(layoutInflater) setContentView(binding.root) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } binding.toolbar.setNavigationOnClickListener { onBackPressedDispatcher.onBackPressed() } val userDetails = intent.getParcelableExtra<ConnectionDetailDTO>("USER_DETAILS") val currentUser = Paper.book().read<User>(Constant.k_user) currentUserId = currentUser?.id!! otherUserId = userDetails?.id!! binding.toolbar.title = userDetails.name Glide.with(this).load(userDetails.profileUrl).placeholder(R.drawable.ic_dummy_user).into(binding.ivProfile) binding.ivProfile.setOnClickListener{showProfile()} adapter = ChatAdapter(currentUserId) binding.rvMessages.layoutManager = LinearLayoutManager(this) binding.rvMessages.adapter = adapter scrollToBottom() val database = FirebaseDatabase.getInstance() val reference = database.getReference("Conversations") reference.addValueEventListener(object : ValueEventListener { override fun onDataChange(dataSnapshot: DataSnapshot) { val messages = mutableListOf<Message>() for (conversationSnapshot in dataSnapshot.children) { val conversationIdParts = conversationSnapshot.key?.split("_") if (conversationIdParts?.size == 3) { val firstUserId = conversationIdParts[1] val secondUserId = conversationIdParts[2] convoId = "conversation_${firstUserId}_${secondUserId}" if (firstUserId == currentUserId.toString() && secondUserId == otherUserId.toString() || firstUserId == otherUserId.toString() && secondUserId == currentUserId.toString()) { val conversationMessages = mutableListOf<Message>() for (messageSnapshot in conversationSnapshot.child("messages").children) { val content = messageSnapshot.child("content").value.toString() val date = messageSnapshot.child("date").value.toString() val messageId = messageSnapshot?.child("id")?.value.toString() val isRead = messageSnapshot.child("isRead").value as? Boolean ?: false val name = messageSnapshot.child("name").value.toString() val senderEmail = messageSnapshot.child("senderEmail").value.toString() val type = messageSnapshot.child("type").value.toString() val message = Message(content, date, messageId, isRead, name, senderEmail, type) conversationMessages.add(message) Log.d(TAG, "onDataChange: $message") } messages.addAll(conversationMessages) } } } Log.d(TAG, "onDataChange: $messages") adapter.updateMessages(messages) } override fun onCancelled(databaseError: DatabaseError) { println("Error: ${databaseError.message}") } }) binding.ivSend.setOnClickListener { val messageText = binding.tiMessage.editText?.text.toString().trim() if (messageText.isNotEmpty()) { val conversationId1 = "conversation_${currentUserId}_${otherUserId}" val conversationId2 = "conversation_${otherUserId}_${currentUserId}" reference.child(conversationId1).get().addOnCompleteListener { task -> if (task.isSuccessful) { val data = task.result?.value val conversationId = if (data != null) { conversationId1 } else { conversationId2 } val messageReference = reference.child(conversationId).child("messages").push() val message = Message( content = messageText, date = SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", Locale.getDefault() ).format(Date()), id = messageReference.key!!, isRead = false, name = currentUser?.name ?: "", senderEmail = currentUserId.toString(), type = "text" ) messageReference.setValue(message) binding.tiMessage.editText?.setText("") scrollToBottom() } } addUser(currentUserId.toString()) } } } private fun scrollToBottom() { binding.rvMessages.scrollToPosition(adapter.itemCount -1) } } 我尝试了 StackOverFLow 和 Chat GPT 的回收器视图解决方案 回收视图开始从上到下滚动,但我希望它是 就像聊天一样,从下到上。添加新消息后,它会滚动到 底部,但在现有列表的最后一项上,而不是在新项目上。 我尝试过使用.. recyclerView 从顶部开始并且不会在新项目上滚动到底部的原因是因为您在将项目添加到适配器之前调用了scrollToBottom()。 删除scrollToBottom()中的binding.ivSend.setOnClickListener{}并将其添加到adapter.updateMessages(messages)下面 Log.d(TAG, "onDataChange: $messages") adapter.updateMessages(messages) scrollToBottom() 这样,当首次从数据库加载项目以及添加新项目时,回收器视图将向下滚动。 为了更容易阅读代码,我建议您将代码排序到方法中,而不是将所有内容都放在 override fun onCreate(savedInstanceState: Bundle?) {} 中。 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() binding = ActivityChatBinding.inflate(layoutInflater) setContentView(binding.root) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } setArgs() setupUI() setupDatabase() setupListeners() } private fun setArgs(){ userDetails = intent.getParcelableExtra<ConnectionDetailDTO>("USER_DETAILS") currentUser = Paper.book().read<User>(Constant.k_user) } private fun setupUI(){ //toolbar binding.toolbar.setNavigationOnClickListener { onBackPressedDispatcher.onBackPressed() } binding.toolbar.title = userDetails.name Glide.with(this).load(userDetails.profileUrl).placeholder(R.drawable.ic_dummy_user).into(binding.ivProfile) binding.ivProfile.setOnClickListener{showProfile()} //recycler view adapter = ChatAdapter(currentUser.id) binding.rvMessages.layoutManager = LinearLayoutManager(this) binding.rvMessages.adapter = adapter } private fun setupDatabase(){ database = FirebaseDatabase.getInstance() reference = database.getReference("Conversations") } private fun setupListeners(){ reference.addValueEventListener(object : ValueEventListener { override fun onDataChange(dataSnapshot: DataSnapshot) { val messages = mutableListOf<Message>() for (conversationSnapshot in dataSnapshot.children) { val conversationIdParts = conversationSnapshot.key?.split("_") if (conversationIdParts?.size == 3) { val firstUserId = conversationIdParts[1] val secondUserId = conversationIdParts[2] convoId = "conversation_${firstUserId}_${secondUserId}" if (firstUserId == currentUser.id.toString() && secondUserId == currentUser.id.toString() || firstUserId == userDetails.id.toString() && secondUserId == currentUser.id.toString()) { val conversationMessages = mutableListOf<Message>() for (messageSnapshot in conversationSnapshot.child("messages").children) { val content = messageSnapshot.child("content").value.toString() val date = messageSnapshot.child("date").value.toString() val messageId = messageSnapshot?.child("id")?.value.toString() val isRead = messageSnapshot.child("isRead").value as? Boolean ?: false val name = messageSnapshot.child("name").value.toString() val senderEmail = messageSnapshot.child("senderEmail").value.toString() val type = messageSnapshot.child("type").value.toString() val message = Message(content, date, messageId, isRead, name, senderEmail, type) conversationMessages.add(message) Log.d(TAG, "onDataChange: $message") } messages.addAll(conversationMessages) } } } Log.d(TAG, "onDataChange: $messages") adapter.updateMessages(messages) } override fun onCancelled(databaseError: DatabaseError) { println("Error: ${databaseError.message}") } }) binding.ivSend.setOnClickListener { val messageText = binding.tiMessage.editText?.text.toString().trim() if (messageText.isNotEmpty()) { val conversationId1 = "conversation_${currentUser.id}_${userDetails.id}" val conversationId2 = "conversation_${userDetails.id}_${currentUser.id}" reference.child(conversationId1).get().addOnCompleteListener { task -> if (task.isSuccessful) { val data = task.result?.value val conversationId = if (data != null) { conversationId1 } else { conversationId2 } val messageReference = reference.child(conversationId).child("messages").push() val message = Message( content = messageText, date = SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", Locale.getDefault() ).format(Date()), id = messageReference.key!!, isRead = false, name = currentUser?.name ?: "", senderEmail = currentUser.id.toString(), type = "text" ) messageReference.setValue(message) binding.tiMessage.editText?.setText("") scrollToBottom() } } addUser(currentUser.id.toString()) } } } 最后我建议您阅读 MVVM 设计模式以获取更多代码结构。

回答 1 投票 0

使用AJAX改变内部HTML有什么好处?

我有代码创建一个新的XMLHttpRequest来更改元素的innerhtml以切换页面。然而,我意识到简单地改变 insidehtml 的效果是完全相同的。所以我很奇怪...

回答 1 投票 0

Java XML:在特定位置插入

我尝试在特定位置插入 XML 节点。 我在插入之前有什么 1 2 ...

回答 1 投票 0

Powershell 创建多达 50k xml 文件最终导致内存泄漏

我必须循环遍历一个包含最多 58k 行的 csv 文件。每行有 60 个分号分隔的值。 我必须为每一行创建一个 xml 文件。我的脚本正在做我期望的事情......

回答 1 投票 0

如果我使用png,如何减少图像按钮的图像?

<ImageButton android:id="@+id/backbutton" android:layout_width="65dp" android:layout_height="50dp" android:layout_gravity="center" app:layout_constraintLeft_toRightOf="parent" app:srcCompat="@drawable/backarrow"/> 我收到了此代码,但按钮上的图像显得太大,以至于剪切了照片的某些部分。我只想知道是否有任何方法可以减少背景的宽度和高度? 我已经尝试过android:scaleType="fitCenter"但没有成功 嗨,转到名为“backarrow”的 xml 文件。您是否尝试过更改其中的测量参数?我举了下面的例子。 <vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"> <path android:fillColor="#ffffff" android:pathData="M12.5,5A7.5,7.5 0 0,0 5,12.5A7.5,7.5 0 0,0 12.5,20A7.5,7.5 0 0,0 20,12.5A7.5,7.5 0 0,0 12.5,5M7,10H9A1,1 0 0,1 10,11V12C10,12.5 9.62,12.9 9.14,12.97L10.31,15H9.15L8,13V15H7M12,10H14V11H12V12H14V13H12V14H14V15H12A1,1 0 0,1 11,14V11A1,1 0 0,1 12,10M16,10H18V11H16V14H18V15H16A1,1 0 0,1 15,14V11A1,1 0 0,1 16,10M8,11V12H9V11" /> 如果这里的高度和宽度对您没有帮助,您可以尝试视口的。

回答 1 投票 0

使用 cs 类更改解析后的 XML 中的值莫比迪克

我有一个名为 Data.xml 的 xml 文件,其内容为: 莫比迪克 赫尔曼梅尔维尔...

回答 1 投票 0

为什么我的卡片视图在 Android Studio 中超出了整个宽度?

我正在使用此卡片视图(蓝色)和回收器视图中的另一个卡片视图来显示发送和接收的消息。 但是卡片视图(蓝色)覆盖了整个宽度,正如您在图片中看到的那样,我无法...

回答 1 投票 0

ElementNSImpl 类型的对象为 com.sun.org.apache.xerces.internal.dom.ElementNSImpl

所以我部署在 JDK1.8.0_161 环境上的 WS-Trust 服务器正在等待一个包含 org.apache.xerces.dom.ElementNSImpl 类型的 xml 节点的请求,但是当请求到来时,节点......

回答 2 投票 0

在具有约束布局的 AlertDialog 中使用时,回收器视图布局宽度很奇怪

我正在尝试在 AlertDialog 中使用回收器视图。 我希望回收器视图占据 AlertDialog 宽度的 100%。 我使用这个xml代码 ...

回答 1 投票 0

无法加载文件或程序集“DocumentFormat.OpenXml

我一直收到这个错误是VS2013 无法加载文件或程序集“DocumentFormat.OpenXml,Version=2.0.5022.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。 在...

回答 9 投票 0

使用 XML 创建 URL

我搜索了又搜索,似乎找不到任何关于如何编写我自己的严格 XML 网页的教程。这就是我想自己创造的东西。我对 XML 不太了解...

回答 5 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.