初のアンドロイドアプリ作成に挑戦しています。
Androidスタジオは、キリンバージョンです。
Android21以上をカバーするモードでプロジェクトを作成しました。
MainActivity.ktに、
package com.websarva.wings.android.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.websarva.wings.android.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MyApplicationTheme {
Greeting("Android")
}
}
と書かれています。これにより、Hello Androidが表示されているというのはなんとなくわかります。
続いて、参考書に書かれている通りに、
res/values/strings.xml
に、下記内容を記入。
<resources>
<string name="app_name">画面部品サンプル</string>
<string name="tv_msg">お名前を入力してください。</string>
</resources>
参考書の指示、res/layoutフォルダがないので、新規作成。
その下に、axtivity?main.xmlをテキストファイルで新規作成し、その中に下記の内容を記入しました。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a1a9ba"
android:orientation="vertical" >
<TextView
android:id="@+id/tvlobelInput"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:text="@string/tv_msg"
android:textSize="24sp"/>
</LinearLayout>
これでエミュレーターにて実行しましたが、「画面部品サンプル」「お名前を入力してください。」(★)は表示されず、Hello Andorid!のみが表示されます。
どうやったら、★部分が表示されるようになるでしょうか。
A 回答 (1件)
- 最新から表示
- 回答順に表示
No.1
- 回答日時:
問題を解決するためには、MainActivity.ktでTextViewを追加して、@string/tv_msgに対応するテキストを表示する必要があります。
MainActivity.ktに以下のコードを追加してみてください。
kotlin
Copy code
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.Center
) {
Greeting("Android")
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(id = R.string.tv_msg),
style = MaterialTheme.typography.h5
)
}
}
}
}
}
}
上記のコードでは、Columnを使ってGreetingの下に新しいTextViewを追加しています。そのTextViewのテキストはstringResourceを使用してR.string.tv_msgに対応する文字列を取得しています。
また、必要に応じてimport文を追加することも忘れずにしてください。
これにより、エミュレーター上で「Hello Android!」と「お名前を入力してください。」が表示されるはずです。
ありがとうございます。
Android resource linking failed
com.websarva.wings.android.myapplication.app-main-46:/layout/activity_main.xml:9: error: resource string/table_layout_4_open (aka com.websarva.wings.android.myapplication:string/table_layout_4_open) not found.
com.websarva.wings.android.myapplication.app-main-46:/layout/activity_main.xml:13: error: resource string/table_layout_4_open_shortcut (aka com.websarva.wings.android.myapplication:string/table_layout_4_open_shortcut) not found.
com.websarva.wings.android.myapplication.app-main-46:/layout/activity_main.xml:19: error: resource string/table_layout_4_save (aka com.websarva.wings.android.myapplication:string/table_layout_4_save) not found.
com.websarva.wings.android.myapplication.app-main-46:/layout/activity_main.xml:23: error: resource string/table_layout_4_save_shortcut (aka com.websarva.wings.android.myapplication:string/table_layout_4_save_shortcut) not found.
error: failed linking file resources.
追加しただけだと、エラーが出てしまいました。調整、やってみます。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- JavaScript 2段階プルダウンで1段階目の選択を終えた後に選択ボックスを見えなくしたい 2 2022/07/05 21:58
- PHP PHP MySql 画像を取得 1 2022/06/04 14:05
- Bluetooth・テザリング ios専用のbluetooth機器をandroidで接続する方法 1 2022/11/04 22:53
- アプリ android studioエラー 1 2022/03/24 17:07
- その他(パソコン・スマホ・電化製品) ios専用のbluetooth機器をandroidで接続する方法 4 2023/01/09 16:50
- VPN 何これ 1 2022/04/19 01:32
- HTML・CSS 書籍を見つつサイト造りの練習をしているのですが、見た目が一致しません 2 2022/11/28 15:00
- JavaScript jQueryでのドラッグアンドドロップについて 1 2022/07/30 09:10
- Android(アンドロイド) スマホバッテリーの節約 画面に表示されるのを三つのアプリだけにして 機内モードはオンにして wi-f 4 2023/02/12 13:08
- HTML・CSS CSS のみのタブ切り替えについて 1 2023/01/11 16:47
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
「オプション機能」が無くなった
-
文章を自動で整えてくれるアプリ
-
同性の友達ができるアプリわか...
-
Androidってなぜアプリや場面に...
-
写真のポーズ変える事が出来る...
-
ゲーム、アプリとかってメール...
-
ファミペイのアプリから選択で...
-
iPhone11を使ってます。 楽天銀...
-
iOSの広告ブロックアプリ
-
メモ帳アプリ
-
アプリで作った単語帳を紙に印...
-
少しお伺いしたい義が御座いま...
-
iPhoneで時計アプリ、メッセー...
-
マッチングアプリで知り合って...
-
先日タイのバンコクに行った際...
-
履歴書の作成で志望動機の作成...
-
iPhoneのファイルアプリは互換...
-
パソコンにて。アプリのインス...
-
パソコンアプリのsophosとは何...
-
Windows10アプリのアンインスト...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Android studio 初心者 Hello A...
-
Windows10でアンドロイドアプリ...
-
データベースはどこにある。
-
oppo reno 7A
-
フォルダーに緑のレ点と赤の✖が...
-
日本に、Microsoftのような会社...
-
インストールが不要なソフトは...
-
Linuxでの開発環境構築や設定の...
-
gcc13.2のバグ?
-
特定ユーザに対してのみアプリ...
-
windows10のアプリと機能にyout...
-
回復ドライブに保存されるもの
-
WindowsDefender以外のウイルス...
-
Beckyのアドレス帳を上下に移動...
-
ペイント(mspaint.exe)がない...
-
vbsでデバッグしながらコードを...
-
Inkscape プリントマークでエラー
-
VC++にてボタン等のサイズ取得
-
VB.net webアプリケーション 戻...
-
ユーザー毎にProgram Files
おすすめ情報
申し訳ないです。先に書いたお礼の内容はこちらのミスで、訂正しました。
いただいた内容を追加しましたが、色々と問題が出ているので、やるべきことを調べてみます。
お手数をお掛けして申し訳ないです。
ありがとうございました。