質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

2回答

3369閲覧

androidstudioでactivity_main.xmlでtextviewが表示されない

MKob

総合スコア4

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2018/07/22 06:43

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
全くの初心者ですが、アンドロイドでボタン、テキストを入れたアプリケーションを作りたいと思っています。androidstudioをインストールし、エミュレーターも動かすことができたので、さっそくテキストを入れ始めようとしたのですが、activity_main.xmlで初めから設定されているHELLOWORLDも表示されません。

発生している問題・エラーメッセージ

This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.``` ### 該当のソースコード ```ここに言語名を入力 ソースコード

試したこと

エラーコードがいくつか出たのですが、ネット情報でエラーコード最後にFIXというボタンを押すと自動的に解決するよとあったので、それを押してみました。水平方向の固定ができていないというメッセージもあったので、それもtextに追加しました。テキストのほうはエラー表示がなくなったのですが、パレットのほうには表示されません。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

activity_main.xmlの内容にいろいろまずい点があるようです。rootとなるConstraintLayoutの幅(android:layout_width)と高さ(android:layout_height)がwrap_contentになっていること、TextViewの高さと幅が1dpになっていることがまず問題ですし、他にもいろいろ不都合な点が見られます。とりあえず下記のようにしてみたら表示されるのではないでしょうか?

XML

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 10 <TextView 11 android:id="@+id/textView3" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="@string/textview" 15 app:layout_constraintBottom_toTopOf="@+id/button" 16 app:layout_constraintEnd_toEndOf="parent" 17 app:layout_constraintStart_toStartOf="parent" 18 app:layout_constraintTop_toTopOf="parent" 19 app:layout_constraintVertical_chainStyle="spread" /> 20 21 <Button 22 android:id="@+id/button" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="Button" 26 app:layout_constraintBottom_toBottomOf="parent" 27 app:layout_constraintEnd_toEndOf="parent" 28 app:layout_constraintStart_toStartOf="parent" 29 app:layout_constraintTop_toBottomOf="@+id/textView3" /> 30 31</android.support.constraint.ConstraintLayout>

ConstraintLayoutは、XMLを直接編集することで思ったような配置にするのは結構難しく、GUIのエディターを使って編集するのが望ましいです。ConstraintLayout自体、入門段階で使いこなすには癖のあるレイアウトなので、慣れるまではRelativeLayoutなど他のものをルートにするのも一つの方法でしょう。

投稿2018/07/22 08:33

編集2018/07/22 10:11
keicha_hrs

総合スコア6766

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MKob

2018/07/22 09:24

ありがとうございます。 残念ながら教えていただいたものでも表示ができませんでした。 もともとlayoutを使ってやっていたのですが、表示ができなかったので手入力を一部していました。 layoutではアプリ名も表示ができていない(エミュレーターではアプリ名もhelloworldも表示ができているので、そもそもどこかがおかしいのかもしれません。 androidstudioの入門書を見ながらやっているのですが、何がいけないのか判明できていません。 ボタンを1か所作りたいだけなのですが、、、、、
keicha_hrs

2018/07/22 10:18

レイアウトエディター上でツールバー(アプリ名が表示されているバー)が表示されていないのは、サポートライブラリーの問題かもしれません。app/build.gradleを開いて、以下の3箇所を修正してみてください。 compileSdkVersion 27 // 28から27に targetSdkVersion 27 // 28から27に implementation 'com.android.support:appcompat-v7:27.1.1' // 28.0.0-alpha3から27.1.1に API 27のSDK Platformがインストールされていないと、同期するときにインストールするようにメッセージが出るはずなので、それに従って追加インストールしてください。その後にレイアウトエディターを開いてみたらどうでしょうか。
guest

0

activity_main.xml の内容と、クラスファイル(.java)の内容はどうなっていますか?

投稿2018/07/22 06:55

mobaroid

総合スコア21

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MKob

2018/07/22 07:30

<?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="wrap_content" android:layout_height="wrap_content" tools:context=".MainActivity" tools:layout_editor_absoluteY="25dp"> <TextView android:id="@+id/textView3" android:layout_width="1dp" android:layout_height="1dp" android:text="@string/textview" app:layout_constraintBottom_toTopOf="parent" app:layout_constraintEnd_toStartOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" tools:layout_editor_absoluteX="201dp" tools:layout_editor_absoluteY="180dp" /> これが内容です。 超初心者なのです正しくお返事している自信がないのですが、クラスファイルはこうなっています。 package com.example.c_cen.botan1 import android.support.v7.app.AppCompatActivity import android.os.Bundle class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問