Last active
November 11, 2020 22:52
-
-
Save reline/39cc011a07445bd59163771cf8e50ce6 to your computer and use it in GitHub Desktop.
Sample for rendering a list of dictionary entries using Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun DictionaryEntries(words: List<Word>) { | |
Column(modifier = Modifier.padding(all = 16.dp), verticalArrangement = Arrangement.spacedBy(32.dp)) { | |
for (word in words) { | |
Card(elevation = 2.dp, modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(2.dp)) { | |
DictionaryEntry(word.rubies) | |
} | |
} | |
} | |
} | |
@Composable | |
fun DictionaryEntry(rubies: List<Ruby>) { | |
Row(verticalGravity = Alignment.Bottom, modifier = Modifier.padding(8.dp)) { | |
for (ruby in rubies) { | |
Reading(ruby.first, ruby.second) | |
} | |
} | |
} | |
@Composable | |
fun Reading(japanese: String, okurigana: String?) { | |
Column(verticalArrangement = Arrangement.Bottom, horizontalGravity = Alignment.CenterHorizontally) { | |
if (okurigana != null) { | |
Text(text = okurigana, modifier = Modifier.gravity(Alignment.CenterHorizontally)) | |
} | |
Text(text = japanese, fontSize = 40.sp) | |
} | |
} | |
data class Ruby(val first: String, val second: String? = null) | |
data class Word(val rubies: List<Ruby>) | |
val words = listOf( | |
Word(listOf( | |
Ruby("今日", "こんにち"), Ruby("は") | |
)), | |
Word(listOf( | |
Ruby("話", "はなし") | |
)), | |
Word(listOf( | |
Ruby("落", "らく"), Ruby("葉", "よう") | |
)), | |
Word(listOf( | |
Ruby("美", "うつく"), Ruby("しい", null) | |
)), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should realistically support centering 4 hiragana above a single kanji, then here is a list of exceptions over that ratio.
Keep in mind that some words are also very long, so just making the kanji larger may not be viable...