2017-05-15 37 views
0

免責事項:私はAndroidに新しく、自分の道を探しています - Fragmentsは私を混乱させます。Android - 別の静的アクティビティ内に静的アクティビティを表示

私は最終的にタブレイアウトを持つ小さな再開アプリケーションを構築するために取り組んでいます。今のところ私はイメージと名前のついたヘッダーを表示するフロントページと、要約文が短いTextViewと、 "コアスキル"のリストの下に取り組んでいます。 ListViewを私のMainActivity.javaに追加しようとすると、私は同じ問題にぶち当たっています。それは一番下に表示されるのではなく、他のすべての要素に貼り付けられます。

Fragmentを使用せずにListViewを下部に表示する方法はありますか? Fragmentを使用する必要がある場合は、そこに静的なリストを入れるだけの簡単なコードは何ですか?

import android.os.Bundle; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.widget.ArrayAdapter; 
 
import android.widget.ListView; 
 
import android.widget.TextView; 
 

 
public class MainActivity extends AppCompatActivity { 
 

 

 
    private ArrayList<String[]> coreSkills = new ArrayList<String[]>(); 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 

 
     //set the content view of the activity 
 
     setContentView(R.layout.activity_main); 
 

 
     //set a text view for the Summary 
 
     TextView profileSummary = (TextView) findViewById(R.id.summary_text); 
 
     profileSummary.setText(R.string.profile_summary); 
 

 
     String[] coreSkills = { 
 
       "Android Development", 
 
       "GitHub", 
 
       "Skill", 
 
       "Skill", 
 
       "Skill", 
 
       "Skill" 
 
     }; 
 

 
     ArrayAdapter<String> coreSkillsAdapter = 
 
       new ArrayAdapter<String>(this, R.layout.core_skills_list, 
 
         R.id.skill_name, coreSkills); 
 

 
     ListView coreSkillsList = new ListView(this); 
 
     setContentView(coreSkillsList); 
 
     coreSkillsList.setAdapter(coreSkillsAdapter); 
 

 
    } 
 
}
<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/activity_main" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical" 
 
    > 
 

 
    <RelativeLayout 
 
     android:id="@+id/top_info_bar" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="112dp" 
 
     android:background="@color/colorPrimary"> 
 

 

 
     <ImageView 
 
      android:id="@+id/photo" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_centerHorizontal="true" 
 
      android:layout_centerVertical="true" 
 
      android:src="@drawable/ic_face_white_48dp" /> 
 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@id/photo" 
 
      android:layout_centerHorizontal="true" 
 
      android:text="Your Name" 
 
      android:textColor="@color/textWhite" 
 
      android:textSize="24sp" /> 
 

 
    </RelativeLayout> 
 

 
    <RelativeLayout 
 
     android:id="@+id/bottom_info_bar" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="24dp" 
 
     android:background="@color/colorPrimaryLight"> 
 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" /> 
 

 
    </RelativeLayout> 
 

 
    <LinearLayout 
 
     android:id="@+id/main_content" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:orientation="vertical" 
 
     android:paddingLeft="16dp" 
 
     android:paddingRight="16dp" 
 
     android:paddingTop="16dp"> 
 

 
     <TextView 
 
      android:id="@+id/summary_heading" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:paddingBottom="16dp" 
 
      android:text="Summary" 
 
      android:textColor="@color/textBlack" 
 
      android:textSize="18sp" /> 
 

 
     <TextView 
 
      android:id="@+id/summary_text" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:paddingBottom="16dp" 
 
      android:textColor="@color/textBlack" 
 
      android:textSize="14sp" /> 
 

 
     <TextView 
 
      android:id="@+id/core_skills_heading" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:paddingBottom="16dp" 
 
      android:text="Core Skills" 
 
      android:textColor="@color/textBlack" 
 
      android:textSize="18sp" /> 
 

 
    </LinearLayout> 
 

 
    <include layout="@layout/core_skills_list" /> 
 

 
</LinearLayout>
私は activity_main.xmlを通じて実行されているすべての情報を持ってしようとすると、私のアプリは、起動時にクラッシュするので、私は2つのレイアウトファイルで働いています。どちらのファイルも、ネストされた子を持つ LinearLayoutsです。

ありがとうございます!

答えて

0

フラグメントビューにリストビューを追加していません。アクティビティのコンテンツビューをリストビューに置き換えます。

setContentView(coreSkillsList); 

この場合、実際にはフラグメントは必要ありません。代わりに次のようにすることができます。

activity_mainレイアウトの最後にListViewビューを追加します。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    .... 

    <ListView 
      android:id="@+id/my_list_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

</LinearLayout> 

そして、このようなあなたの活動にもアクセス:

.... 
ListView coreSkillsList = (ListView) findViewById(R.id.my_list_view); 
coreSkillsList.setAdapter(coreSkillsAdapter); 
.... 
+0

それを固定こと!私はいくつかの時点でXMLにListViewを追加しようとしましたが、その後、各リストアイテムは**全体の**レイアウトを複製することになりました。ヘッダー、サマリー、リストアイテムを取得します。ヘッダー、サマリー、リストアイテムなど。 もしあなたのListViewスニペットは、マイナスsetContentView以外のものと同じです。それは何を達成するか?それはうまくいった、なぜ私はちょうど混乱している。 おかげで再び! – IronAnneBonnie

+0

@IronAnneBonnieおそらく、複数の 'ListView'ビューを作成していました。 – Titus

+0

@IronAnneBonnie 'setContentView'はアクティビティのレイアウトを設定します。私が行った変更は、アクティビティのレイアウトとして設定するのではなく、アクティビティのレイアウトにリストビューを追加します。 – Titus

関連する問題