2016-01-13 18 views
1

我有一個Java類和兩個XML文件,它們基本上只是一個並排的listView,它有一些值。我想在每個列表視圖上方放置一個標題或一個文本視圖(它們應該保持並排),但是每次嘗試添加textView時,整個項目都會變得混亂,並且下面的聯機指令無法實現標題。在2列表視圖上添加標題/ TextView

public class Leaderboard extends AppCompatActivity { 
     private ListView UsernameList; 
     private ListView ScoreList; 

    LocalDatabase localDatabase; 

    /** Called when the activity is first created */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_leaderboard); 

     ArrayAdapter<String> listAdapter; 
     ArrayAdapter<Integer> list2Adapter; 

     //Find the ListView resources 
     UsernameList = (ListView) findViewById(R.id.UsernameList); 
     ScoreList = (ListView) findViewById(R.id.ScoreList); 

     //Create and populate the list of usernames. 
     //This is where the information from the Database would need to be entered. and the String[] removed 
     String[] usernames = new String[]{"Andy", "Marie", "George"}; 
     ArrayList<String> usernameList = new ArrayList<String>(); 
     usernameList.addAll(Arrays.asList(usernames)); 

     //Create and populate the list of scores 
     //This is where the information from the Database would need to be entered. and the Integer[] removed 
     Integer[] scores = new Integer[]{7, 4, 1}; 
     ArrayList<Integer> scoreList = new ArrayList<Integer>(); 
     scoreList.addAll(Arrays.asList(scores)); 

     //adds the users details to the leaderboard 
     localDatabase = new LocalDatabase(this); 
     Contact contact = localDatabase.getLoggedInUser(); 
     //Set a string to have the value of the users name. 
     String s = contact.name; 
     //Create Array Adapter using the username list 
     listAdapter = new ArrayAdapter<String>(this, R.layout.activity_row, usernameList); 
     //Add more users 
     listAdapter.add(s); 
     //Set the Array Adapter as the ListView's adapter 
     UsernameList.setAdapter(listAdapter); 

     //Create Array Adapter using the username list 
     list2Adapter = new ArrayAdapter<Integer>(this, R.layout.activity_row, scoreList); 
     //Add more users 
     list2Adapter.add(0); 
     //Set the Array Adapter as the ListView's adapter 
     ScoreList.setAdapter(list2Adapter); 
    } 
} 

然後兩個XML文件

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/colorPrimaryDark" 
    android:id="@+id/rowTextView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:padding="10dp" 
    android:textSize="16sp"> 
</TextView> 



<?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:layout_width="fill_parent" 
    android:background="@color/colorPrimaryDark" 
    android:layout_height="fill_parent" 
    android:padding="20dp" 
    android:orientation="horizontal" 
    tools:context=".Leaderboard"> 

    <ListView 
     android:id="@+id/UsernameList" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:layout_weight=".60" 
     android:drawSelectorOnTop="false" 
     android:headerDividersEnabled="false"> 
    </ListView> 

    <ListView 
     android:id="@+id/ScoreList" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:layout_weight=".40"> 
    </ListView> 
</LinearLayout> 

回答

0

嘗試在LinearLayout中封裝兩個TextViews。線性佈局應該跨越整個寬度(match_parent),但只有需要的高度;包裝內容。

然後,您希望如何讓它們在屏幕寬度之間整齊地分開,首先將LinearLayout方向設置爲水平。然後,你創建TextViews的0dp(這是正確的,0dp)的寬度,以及它們的高度wrap_content。然後,給這兩個TextView的權重1.視圖現在都佔用LinearLayout的一半空間(寬度)。最後,將TextViews放在一個好看的標題中。

我保持它非常廣泛和非代碼化,但我認爲你應該能夠谷歌上面解釋的所有概念,如果你遇到任何麻煩。如果你真的無法弄清楚,我會在評論中幫助你。