我的页面中有三个布局。两个线性布局,表示其中包含信息的2个条带,以及第三个线性布局中的列表视图。我成功地将其中一个线性布局连接到页面顶部,但我希望第二个linearlayout(底部控制条)附加到列表视图的底部,而不是屏幕底部。有人可以帮我解决这个问题......这是我的XML代码。
<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:background="@color/grey05"
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.albert.betterapp.MyBets">
<LinearLayout
android:id="@+id/top_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#C9C193"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Accumulator(5)"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Token:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView121"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="top"
android:layout_weight="0.2"
android:layout_marginBottom="-5dp"
android:includeFontPadding="false"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/wingreen"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#C9C193"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="@string/stake"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="500"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="top"
android:layout_weight="0.2"
android:layout_marginBottom="-5dp"
android:includeFontPadding="false"
android:text="@string/potentialwinnings"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/wingreen"
android:textStyle="bold" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="6000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_above="@id/bottom_control_bar"
android:layout_alignParentRight="true"
android:layout_below="@id/top_control_bar"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@+id/betslistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/betcolor"></ListView>
</LinearLayout>
答案 0 :(得分:2)
好的,所以我使用了两种不同的布局。一个用于页脚(当列表视图不填充整个屏幕时),一个用于底部视图(当列表视图大于屏幕时)。请记住,它们是两个不同的视图,但相同。这意味着您需要为每个项目提供不同的ID。
首先是截屏:
ListView不会填充整个屏幕
ListView会填充整个屏幕
<强> activity_main.xml中强>
<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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/top_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#C9C193"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Accumulator(5)"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Token:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView121"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="top"
android:layout_marginBottom="-5dp"
android:layout_weight="0.2"
android:includeFontPadding="false"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomView"
android:layout_below="@+id/top_control_bar"/>
<!-- The View below should be your menu-->
<LinearLayout
android:id="@+id/bottomView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#C9C193"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Stake"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="500"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="top"
android:layout_marginBottom="-5dp"
android:layout_weight="0.2"
android:includeFontPadding="false"
android:text="potentialwinnings"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="6000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
</LinearLayout>
</RelativeLayout>
<强> footer.xml 强>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#C9C193"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Stake"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="500"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="top"
android:layout_marginBottom="-5dp"
android:layout_weight="0.2"
android:includeFontPadding="false"
android:text="potentialwinnings"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="6000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp"
android:textStyle="bold"/>
</LinearLayout>
<强> MainActivity 强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> items = new ArrayList<String>();
for (int i = 0, z = Cheeses.CHEESES.length; i < z; i++) {
items.add(Cheeses.CHEESES[i]);
}
final MyListAdapter myListAdapter = new MyListAdapter(items);
// BottomView when the list is greater than the screen
final LinearLayout bottomView = (LinearLayout) findViewById(R.id.bottomView); // Bottom View
bottomView.setVisibility(View.GONE);
// FooterView when the list is smaller than the screen
final View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
TextView textView9Footer = (TextView) footerView.findViewById(R.id.textView9); // How you reference the textview in the footer
textView9Footer.setText("Testing One"); // Updating text in the footer
TextView textView9 = (TextView) findViewById(R.id.textView9); // Textview in the bottom view
textView9.setText("Testing Two");
// Set the adapter to start to check to see if the listView is greater than the screen
final ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myListAdapter);
// Runnable to check to see if the list is larger than the screen
Runnable fitsOnScreen = new Runnable() {
@Override
public void run() {
int last = listView.getLastVisiblePosition();
if (last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) { // List Not Full Screen
bottomView.setVisibility(View.GONE);
// Add the footer than reset the adapter to attack the footer
listView.addFooterView(footerView);
} else {
// Does nothing other than make the bottom view visible.
bottomView.setVisibility(View.VISIBLE);
}
}
};
listView.post(fitsOnScreen);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
class MyListAdapter extends ArrayAdapter<String> {
MyListAdapter(ArrayList<String> items) {
super(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, items);
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
View view = super.getView(position, convertView, container);
return view;
}
}
public static class Cheeses {
public static final String[] CHEESES = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
"Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
"Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
"Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
"Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss"
};
public static final String[] CHEESEStwo = {
"Abbaye de Belloc", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
"Abondance", "Abbaye de Belloc"};
}
}
<强>更新强>
可能的解决方案是将其放在PopulateList()
中private void PopulateList() {
ArrayAdapter<BetParameters> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.betslistview);
list.setAdapter(adapter);
final LinearLayout bottomView = (LinearLayout) findViewById(R.id.bottomView); // Bottom View
bottomView.setVisibility(View.GONE);
// FooterView when the list is smaller than the screen
final View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
// Runnable to check to see if the list is larger than the screen
Runnable fitsOnScreen = new Runnable() {
@Override
public void run() {
int last = list.getLastVisiblePosition();
if (last == list.getCount() - 1 && list.getChildAt(last).getBottom() <= list.getHeight()) { // List Not Full Screen
bottomView.setVisibility(View.GONE);
// Add the footer than reset the adapter to attack the footer
list.addFooterView(footerView);
} else {
// Does nothing other than make the bottom view visible.
bottomView.setVisibility(View.VISIBLE);
}
}
};
list .post(fitsOnScreen);
}
答案 1 :(得分:1)
在第二个“线性布局”(底部控制栏)中,删除
android:layout_alignParentBottom="true"
并将其替换为
android:layout_below="@+id/betslistview"
在包含列表视图的第三个“线性布局”中,删除
android:layout_above="@id/bottom_control_bar"