• ベストアンサー

ボタンの場所を固定にしたい

お世話になります。 JAVA eclipse でアンドロイドアプリを作りたいと思っています。 画面.xmlにtextviewとbuttonがある状態です。 textviewの下にbuttionがあるのですが、 buttionの位置がテキストビューの長さによって 変わってしまいます。 buttionを画面の最下部にしたいと 思っているのですが、直接値を入れては、、 スマホの画面サイズによって、画面の最下部の位置が ことなると思います。 どのようにすれば、buttionを画面の最下部に することができるでしょうか? 何卒宜しくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • anmochi
  • ベストアンサー率65% (1332/2045)
回答No.1

textviewとbuttonが配置されてる親コンテナは何かな。それによって方法は変わるんだぜ。 まぁやりかたの一つとして、以下のようなものがある。 ・画面.xmlをRelativeLayoutにする。 ・その中にScrollViewとButtonを置く。 ・Buttonを左下に配置する。widthは幅いっぱいでheightはコンテンツ高。 ・ScrollViewをButtonの上に配置する。widthは幅いっぱいでheightは高さいっぱい。 ・TextViewをScrollViewの中に置く。widthは幅いっぱいでheightはコンテンツ高。 ~~~~画面.xml~~~~ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" (省略) > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/button1" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> </ScrollView> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="@string/hello_world" /> </RelativeLayout> ~~~~~~~~ XMLは上のような感じになる。 もう一つのやりかたは、LinearLayoutを用いてテキスト部とボタン部を2分割する方法だ。こちらの方がやりやすいかも知れない。面倒なのでXMLだけ。ポイントは、2分割する時に片方の高さをきっちり決め(ここではwrap_contentを使ってるけど)てweightを0にし、もう一方をmatch_parentにしてweightを1にするということだ。 ~~~~画面.xml~~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" (省略) > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> </ScrollView> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:text="@string/hello_world" /> </LinearLayout> いずれにせよ、ScrollViewを噛ましている理由はテキストがあふれた時も全部表示できてかつボタン側に浸食しないようにするためだ。

kgyqk433
質問者

お礼

>textviewとbuttonが配置されてる親コンテナは何かな。それによって方法は変わるんだぜ。 書き出しは、”ぜ”っという表現に驚きましたが、 ><LinearLayout xm >いずれにせよ、ScrollViewを噛ましている理由はテキストがあふれた時も全部表示できてか >つボタン側に浸食しないようにするためだ。 渋い、渋すぎます。。。 私が求めていた回答にプラスして、ScrollViewの知識まで!! 本当にありがとうございます。!!!

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • EclipseでEditTextが表示されない!

    はじめまして、よろしくお願いいたします。 EclipseでのAndroidアプリの勉強を始めた者です。 Androidアプリの本に従い、メモ帳アプリを作成中ですが、 EditTextがパレットに表示されません。 ■環境 ・Android 4.0.3 ・Eclipse ■手順 ・main.xmlを開く ・GraphicalLayoutをクリックしパレットを見る。 ・FormWidgetsを見るが、TextViewとかButtonとかはあるものの、EditTextが見当たらない。 ・一応ほかのカテゴリーを見ても同様に見当たらない。 何かEditTextビューを使用するために必要な作業があるのでしょうか? こんな質問で申し訳ございませんが、よろしくお願いいたします。

    • ベストアンサー
    • Java
  • R.javaが自動生成されない

    Androidアプリ開発をEclipseでしていますがR.javaが自動生成されず、 ビルド時にエラーが出てしまいます。 コンパイル時に「Rを変数に解決できません」の主旨のエラーが出ています。 他サイトでの対処方法を参考に以下方法を実行しましたが、 R.javaは自動生成されずに半ばあきらめぎみです… 以下試した方法 --------------------------------------------------------- ○Eclipseのプロジェクトスクリーンを実行する ○「プロジェクト」→「自動的にビルド」にチェックが入っている ○layoutフォルダのXMLの文法エラーはない ○Eclipseの再起動 ○プロジェクトのクリーン ○xmlファイル名はactivity_mainであり大文字はつかっていない ○「ウィンドウ」を選択し「設定」→「Java」→「コンパイラー」 でコンパイラー準拠レベルを変更 --------------------------------------------------------- 以下activity_main.xml ---------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="12pt" /> <Button android:id="@+id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button01" /> </LinearLayout> ---------------------------------------------------------------------- 以下string.xml ---------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Botan</string> <string name="hello">ボタンを押してね。</string> <string name="button01">クリック</string> </resources> ---------------------------------------------------------------------- 以下MainActivity.java ---------------------------------------------------------------------- package com.example.botan; import android.os.Bundle; import android.app.Activity; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class MainActivity extends Activity { private TextView text1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1 = (TextView)this.findViewById(R.id.TextView); Button btn1 = (Button)this.findViewById(R.id.burron01); btn1.setOnClickListener(new MyClickAdapter()); } class MyClickAdapter implements OnClickListener { public void onClick(View view){ text1.setText("クリックしました!"); } } }----------------------------------------------------------------------------------- どなたか他にチェックする箇所、 もしくは上記で不適切な記述があることが分かるかた ご教示下さい。 よろしくお願い申し上げます。 補足 以下参考サイト(ソース等はコピペしたので同じはずです) ▼Androidプログラミング日記 http://androidn.hannnari.com/link7-botan.html

    • ベストアンサー
    • Java
  • Android開発 Eclipse(超初心者)

    Eclipseの使い方がわかりません。 eclipseをインストールしてプロジェクトを作成しAVDの仮想デバイスを適当に作成し AVD仮想デバイス ターゲット:Android4.0-APILevel14 SDカード:サイズ 66MiB スキン:ビルトイン WVGA800 package com.example.android.hello.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class Hello15 extends Activity{ //表示用のテキストとボタンを宣言 --- private TextView txtCaption; private Button btnHello; public void OnCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //レイアウトを作成 LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //テキストを追加 txtCaption = new TextView(this); txtCaption.setText("Click Button!"); layout.addView(txtCaption); //ボタンを追加 btnHello = new Button(this); btnHello.setText("Button"); layout.addView(btnHello); //イベントを設定 btnHello.setOnClickListener(new OnClickListener(){ public void onClick(View v){ txtCaption.setText("Hello, Android!"); } }); } } サンプルで上記のようなソースを作成して 実行したのですが 以前は Android Application Java Applet Java Application など選択肢が出てたのですが仮想デバイスを設定した 辺りからでなくなり実行して少し経つと ←画面に『android』という文字が →画面にキーボードのようなものがでてきて 3分ぐらいまつと←画面が携帯画面に変化します 上記のプログラムではボタンの生成とメッセージがでてくるはずが それらしきものがありません。これらはどこに出てくるのでしょうか? 動かし方すらよくわかってないのでお解説おねがいいたしもうす;

  • 【Android開発】ボタンのテキスト表示

    Android初心者です。現在画面遷移先の画面に配列で設定した質問文、選択肢を表示するアプリを制作しています。質問文はTextView、選択肢は2つのButtonにテキストとして表示する。画面遷移した後にsetQuestion()、setChoiceを呼び出したいんですが実機で試したところ、画面遷移した後に「問題が発生しため、(アプリ名)を終了します」とポップアップが出て、アプリが落ちます。 この2つのメソッドをonCreateに入れるのは間違なのでしょうか?どうすればうまくいきますか?初心者なのでうまく書けてないかも知れません。 よろしくお願いします。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); setChoice(); setQuestion();  } int number = 0; //質問番号 //選択肢 Button button1; Button button2; Button button3; Button button4; //質問文を配列に格納 static String question[] = { "質問文1", "質問文2", }; //選択肢 static String choice[][] = { {"1番", "2番", "3番", "4番"}, {"1番", "2番", "3番", "4番"}, }; //問題の回答 static String answer[] = { "2番", "4番" }; //出力 //質問文を表示 public void setQuestion() { String setQuestion = question[number]; // TextView question = (TextView) findViewById(R.id.Question); question.setText(setQuestion); } //選択肢の表示 public void setChoice() { TextView button1 = (Button) findViewById(R.id.button1); TextView button2 = (Button) findViewById(R.id.button2); button1.setText(choice[number][0]); button2.setText(choice[number][1]); } xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Question" android:id="@+id/Question" android:textSize="40dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="81dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:textSize="30dp" android:onClick="choiceClick" android:layout_above="@+id/button2" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:textSize="30dp" android:onClick="choiceClick" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/button1" android:layout_alignStart="@+id/button1" />

  • string.xmlで宣言した文字について

    お世話になります。 java eclipse でスマホアプリを作成しています。 string.xmlで、 <string name="hello_world">Hello world!</string> と記載し、 画面.xmlで android:hint="@string/hello_world" と記載すると、 Hello world! と表示されます。 これを画面.xmlではなく、.javaの方で記載したいのですが、 どのように記載すればよいでしょうか? ※@string/hello_worldと記載するとエラーになりました お手数ですがよろしくお願いします。

    • ベストアンサー
    • Java
  • Fragmentを使ってリスト表示

    お世話になっております。 androidアプリケーションを作成しています。 画面Aの(1)というボタンを押すと それに半透過された画面B(リスト)が被さる、 といった処理を行いたいです。 現在のソースコードは 画面B.java public class 画面B extends Fragment { public View onCreateView { //中略 } 画面B.xml <FrameLayout //中略 > <ListView/> <TextView/> </FrameLayou> となっています。 画面B.javaにリスト表示部分を付けたそうかと思ったの ですが、 ListActivityのextendができないようなので困っていま す。 現在、画面B.xmlのグラフィカルレイアウトにはリスト とテキストが表示されており、 実機ではテキストのみ確認できる状態です。 リストを表示させるためにはどのような修正が必要か、 教えていただけますと嬉しいです。

  • Androidアプリ・SDKでの実行時エラー

    初めてアンドロイドアプリの開発をしています(多言語のプログラミング経験はありますが、JAVAは初めてです)。Eclipseで開発を行っていますが、ボタンをクリックしたら画面遷移をする、という処理を追加したところ、Android SDKで実行した際に 「アプリ名(パッケージ名)が予期せず停止しました。やり直してください。」 と出て、トップの画面も表示されなくなってしまいました。なお、Eclipse上ではエラーなどが出ていません。原因が解らず困っています。何かわかる方がいらっしゃいましたらお教え下さい。 以下がソースになります。 ■トップのActivity--------------------- package jp.xxxxx.xxxxx; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //ボタンを定義 Button btn1 = (Button) findViewById(R.id.btn1); //クリックイベント btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent = new Intent( getApplicationContext() , SubActivity.class); startActivity(intent); } }); } } ■トップ画面のXML--------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <ImageView android:id="@+id/topLogo" android:src="@drawable/logo" /> <LinearLayout android:id="@+id/linearLayout1"> <TextView android:id="@+id/btn1" android:text="@string/btn1text"></TextView> </LinearLayout> </LinearLayout> ■トップのAndroidManifest.xml--------------------- <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.xxxxx.xxxxx" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name="MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="@string/screenName2" android:name="SubActivity"> </activity> </application> </manifest> なお、Activityの「//ボタンを定義」以下をコメントアウトした際には画面は出てきます。何か初歩的な間違いをしているかもしれませんが、どうかよろしくお願いします。

    • ベストアンサー
    • Java
  • android HashMapについて助言お願いし

    初心者ですが、どうぞ宜しくお願いします。 下記コードでは、マップに 「 map.put( "Key1", "val1" ); map.put( "key2", "val2" ); 」を追加しているのですが、このデータをキーから取り出しテキストビューに表示しようとしています。 このままではエラーはないのですが、何も表示されない状況です。 初心者の無知な質問となりますが、 テキストビューへの表示方法はどのようにすればよいのでしょうか? 何か別の方法でもよいので、ご教授宜しくお願いいたします。 -------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1"></TextView> </LinearLayout> ------------------------------------------------------------ package com.test; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1)HashMapからMapのインスタンスを生成 HashMap<String, String> map = new HashMap<String, String>(); // キーと値のペアを格納 map.put( "Key1", "val1" ); map.put( "key2", "val2" ); // 指定したキーに対応する値を取得. String val = (String)map.get( "key1" ); // テキストビュー取得 TextView tv = (TextView)findViewById(R.id.tv1); tv.setText(val); } }

    • ベストアンサー
    • Java
  • Android実行時のエラー(実機にて)

    Androidアプリ開発初心者の者です。 下記に記載しています書籍のアプリを 作成中にエラーが起こりスマホのアプリが強制終了と いう形で落ちてしまいます。 【アプリを作ろう! ゼロから学ぶアプリの作成から公開まで Android2.3~4.1対応】  http://ec.nikkeibp.co.jp/item/books/P96130.html Eclipseのコード画面は以下のようになっています。 【OmikujiActivity.java】 package makeApplication.omikuji; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OmikujiActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 文字を表示する TextView tv = (TextView) findViewById(R.id.action_settings); tv.setText("おみくじアプリ"); } } 下から4行目のaction_settingsの部分は書籍では補完候補で、 hellow_view:int - R.idがポップアップ表示されているのですが、 現在私のコード画面には表示されず、仕方なくaction_settingsを選択しています。 上記で保存し実行すると、スマホの実機の方で「おみくじ」と表示されず、 そのまま強制終了してしまい再度実行を促されてしまいます。 action_settingsの部分が原因なのかどうかよく 分からないのでお詳しい方ぜひ教えて頂きたいです。 宜しくお願い致します。

    • ベストアンサー
    • Java
  • android TableLayout使用時で

    いつもお世話になっています。 android TableLayout使用時のことで質問します。 forでTextViewに値を入れていきたいのですが、このときxmlでTextViewをひとつ作っておいてそのTextViewを使いまわすことはできないのでしょうか? したいのが、 for文で、TableRowに値を入れていきたい。 private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private final int FP = ViewGroup.LayoutParams.FILL_PARENT; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TableLayout tableLayout = new TableLayout(this); setContentView(tableLayout); for(int a=0;a<3;a++){ TextView text1 = new TextView(this); text1.setText("user"); TableRow tableRow1 = new TableRow(this); tableRow1.addView(text1); tableLayout.addView(tableRow1, createParam(FP, WC)); } } private TableLayout.LayoutParams createParam(int w, int h){ return new TableLayout.LayoutParams(w, h); } この時表示されたのが、 user user user この方法を、xmlのTextViewを呼ぶ形で実現したいのですが、 xmlでは、 <TableRow> ↓ このTextViewを使いまわしたい。 <TextView android:id="@+id/user" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Test"> </TextView> </TableRow> で、表記しています。 xmlでは、普通に先に記述していないと使えないのでしょうか? つまり、もしTableにTextViewを3つ使いたいなら、TextViewを既に3つ記述していないと使えないものなのでしょうか? よろしくお願いします。

    • ベストアンサー
    • Java
このQ&Aのポイント
  • Excel VBAで既定のシートを下に追加する方法を教えてください。現在、注文書シートを増やすごとに、その範囲を下にコピーしています。しかし、50件以上になると処理に時間がかかってしまいます。他の方法をご教示いただけないでしょうか。
  • Excel VBAを使用して、既定のシートの下に新しいシートを追加する方法についてお聞きしたいです。現在、注文書というシートがあり、それを増やすたびに同じ範囲を下にコピーしています。しかし、50件以上になると処理時間が長くなってしまいます。他の効率的な方法はありますか?
  • Excel VBAを使って、既定のシートの下に新しいシートを増やす方法を知りたいです。現在は注文書というシートを増やすたびに、同じ範囲を下にコピーしています。しかし、50件以上になると処理に時間がかかるようになりました。他の効果的な方法はありますか?
回答を見る