我有一个Java类和两个XML文件,它们基本上只是一个并排的listView,它接受一些值。我想在每个列表视图上方放置一个标题或文本视图(它们应该并排放置)但是每次我尝试添加一个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 :(得分:0)
尝试在LinearLayout中封装两个TextView。线性布局应该跨越整个宽度(match_parent),但只能尽可能高; WRAP_CONTENT。
如果要将它们整齐地划分在屏幕宽度之间,那么首先要将LinearLayout方向设置为水平。然后,你创建TextViews 0dp(正确,0dp)和它们的高度wrap_content的宽度。然后,给两个TextViews赋予权重1.现在,视图占据了LinearLayout的一半空间(宽度)。最后,将TextView放在一个好看的标题中。
我保持它非常广泛且非编码,但我认为如果遇到任何麻烦,你应该能够谷歌上面解释的所有概念。如果你真的无法弄明白,我会在评论中帮助你。