2016-09-22 2 views
-1

各TextViewをクリックすると別のレイアウトファイルが表示され、人身売買に関する情報を得ることができます。 onCreateメソッドでは、setOnClickListenerがテキストビューになっています。それを膨らませることは問題です。これはビューを膨らませることと呼ばれていますか?私は人々がsetContentViewを使って、このためにフラグメントの使用を推奨しているのを見たことがあります(これは使わないはずです)。しかし、それは動作しません。このコードはどのように見えるでしょうか?TextViewを使用してレイアウトを拡張する

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.piatt.worksafe.MainActivity"> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Work Safe!" 
    android:textSize="36sp" 
    android:layout_centerHorizontal="true" 
    android:paddingBottom="32dp" 
    android:id="@+id/title" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="What is Human Trafficing?" 
    android:layout_below="@+id/title" 
    android:layout_centerHorizontal="true" 
    android:textSize="24sp" 
    android:padding="16dp" 
    android:id="@+id/whatIsHumanTrafficing" 
    android:clickable="true" 
    /> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="How do I get safe labor?" 
    android:layout_below="@+id/whatIsHumanTrafficing" 
    android:layout_centerHorizontal="true" 
    android:textSize="24sp" 
    android:padding="16dp" 
    android:id="@+id/howDoIGetSafeLabor" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="How do I check that my job/job offer is legal?" 
    android:layout_below="@+id/howDoIGetSafeLabor" 
    android:layout_centerHorizontal="true" 
    android:textSize="24sp" 
    android:padding="16dp" 
    android:gravity="center" 
    android:id="@+id/checkLegality" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="How can I get help?" 
    android:layout_below="@+id/checkLegality" 
    android:layout_centerHorizontal="true" 
    android:textSize="24sp" 
    android:padding="16dp" 
    android:id="@+id/getHelp" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="About us" 
    android:layout_below="@+id/getHelp" 
    android:layout_centerHorizontal="true" 
    android:textSize="16sp" 
    android:layout_alignParentBottom="true" 
    android:gravity="bottom" 
    android:id="@+id/aboutUs" 
    /> 



</RelativeLayout> 

Javaクラス:脇

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TextView whatIsHumanTrafficing = (TextView) findViewById(R.id.whatIsHumanTrafficing); 
    whatIsHumanTrafficing.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view){ 

      //What is this context, why do I need it and where does it come from? 
      //What is the ViewGroup, why do I need it and where does it come from? 
      view.inflate(Context context, R.layout.what_is_human_trafficing, ViewGroup root); 
     } 
    }); 

} 

答えて

0

:あなたは私はなぜ、このような状況で何View.inflate

のドキュメントを読むことができますそれを必要とし、それはどこから来ますか?あなたはビューを膨らませるためのパラメータとして、それを必要とする:なぜYourActivity.this(あなたの実際のクラス名に置き換え)

:何

ViewGroupとは何ですか、なぜそれが必要なのですか、どこから来たのですか?

おそらくそれは必要ありません。 nullでもかまいません。 Viewを展開するのはViewGroupです。 ListViewの場合と同様に、各行を「グループ」としてリストにロードします。あなたが動的にクリックされたものに基づいて、異なるレイアウトファイルを表示したい場合は


はとにかく、はいFragment年代には、それを行うための一つの方法であるので、ViewStubている年代は。

そして、それは静的メソッドであるため、TextViewで実際にinflateを呼び出すことはありません。へのコンテンツビュー設定MainActivity.thisに、その中に、テキストビューをクリックしてリスナーに

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final RelativeLayout rl = (RelativeLayout) findViewById(R.id.splashScreenId); 
    TextView whatIsHumanTrafficing = (TextView) findViewById(R.id.whatIsHumanTrafficing); 
    whatIsHumanTrafficing.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view){ 


      MainActivity.this.setContentView(R.layout.what_is_human_trafficing); 

     } 
    }); 

} 

設定します。最終的なコードは次のようになりますLayoutInflater

View v = LayoutInflater.from(YourActivity.this).inflate(R.layout.your_layout, null); 
// TODO: Do something with v 
+0

私が(R.layout.your_layout)を膨張させると、別のパラメータが必要になります。それは@ Nullable Viewgroupルートを教えてくれます。私はあなたの助けに感謝します。 – Davep

+0

前と同じ回答です。これは 'null'(' @ Nullable'引数を参照)です。 –

+0

nullは現在問題を引き起こしていません。私は相対レイアウト 'rl'への参照を得て、ビュー' rl.addView(v); 'を追加しました。私はアプリを実行すると、前のレイアウト上にテキストを表示します。これをどうすれば解決できますか? – Davep

0

でこれを行うに相当しますあなたが望むレイアウト。

+0

これに対して私は助言するでしょう。 'setContentView'(名前が暗示するように)はコンテンツビューを上書きします。したがって、' findViewById'は確実に動作しなくなります。したがって、以前のコンテンツビューに戻ることは難しい課題です。このようにしたい場合は、フラグメントを交換することをお勧めします。 –

+0

私はちょうど私がバックボタンを押してみたときにこれに遭遇しました。私は明日の破片でそれを実装します。アドバイスをありがとう、これは学習経験をされています。 – Davep

関連する問題