在2列表视图上添加标题/ TextView

在2列表视图上添加标题/ TextView

问题描述:

我有一个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> 

尝试在LinearLayout中封装两个TextViews。线性布局应该跨越整个宽度(match_parent),但只有需要的高度;包装内容。

然后,您希望如何让它们在屏幕宽度之间整齐地分开,首先将LinearLayout方向设置为水平。然后,你创建TextViews的0dp(这是正确的,0dp)的宽度,以及它们的高度wrap_content。然后,给这两个TextView的权重1.视图现在都占用LinearLayout的一半空间(宽度)。最后,将TextViews放在一个好看的标题中。

我保持它非常广泛和非代码化,但我认为你应该能够谷歌上面解释的所有概念,如果你遇到任何麻烦。如果你真的无法弄清楚,我会在评论中帮助你。