我使用的是Android Studio v3.1.2。我有一对带有ListView和按钮的活动。无论ListView中有多少项,该按钮都应该是alignParentBottom。
在我的MainActivity上,ListView和Button在RelativeLayout上完美渲染。我可以向ListView添加大量项目,按钮保持不变。
另一方面,我的JournalTabFragment是一个在TabLayout中使用RelativeLayout的片段。片段由PetDetailTabbedActivity在代码中创建。 Fragment的布局是MainActivity布局的修改版本,但即使ListView不包含数据,该按钮也不会显示。我尝试使用LinearLayout,其权重成功有限 - 按钮出现,但它会浮动在listview内容下方,并在数据添加到ListView时向下移动。然后,当ListView中有多行时,它会垂直展开。这是我尝试过的最新布局:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="8dp"
>
<ListView
android:id="@+id/journallistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/addentrybtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:text="@string/addentrybtn" />
</RelativeLayout>
因为它与MainActivity上的布局非常相似,所以似乎它应该有效,所以我无法帮助但是认为TabLayout与它有关。
我缺少什么?!如何让按钮粘贴到活动布局的底部而不会被压扁? TIA的帮助。这是创建片段的父活动的Java。
public class PetDetailTabbedActivity extends AppCompatActivity implements DeleteConfirmDialog.DeleteConfirmDialogListener {
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
DatabaseHelper myDB;
Integer iPetID = 0;
private int REQUEST_CODE = 1;
JournalTabFragment journalFrag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pet_detail_tabbed_activity);
myDB = new DatabaseHelper(this);
//load the toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//let's give it a back button in the title bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
assert getSupportActionBar() != null;
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
//get the pet ID from the extras
iPetID = bundle.getInt("PetID");
}
}
@Override
protected void onResume() {
super.onResume();
if(iPetID > 0) {
String sPetName = myDB.getPetNameByID(iPetID);
getSupportActionBar().setTitle(sPetName);
}
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter((getSupportFragmentManager()));
String sStatus = myDB.getPetStatusByPetID(iPetID);
journalFrag = new JournalTabFragment();
Bundle jbundle = new Bundle();
jbundle.putInt("PetID", iPetID);
journalFrag.setArguments(jbundle);
PetDetailTabFragment petFrag = new PetDetailTabFragment();
Bundle gbundle = new Bundle();
gbundle.putInt("PetID", iPetID);
petFrag.setArguments(gbundle);
//add the tabs
adapter.addFragment(journalFrag, "Journal");
adapter.addFragment(petFrag,"Details");
viewPager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.detailsmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.home:
goHome();
return true;
case R.id.edit:
openEditActivity();
return true;
case R.id.delete:
deletePet();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void goHome() {
Intent intent = new Intent(PetDetailTabbedActivity.this, MainActivity.class);
startActivity(intent);
}
private void openEditActivity() {
Intent editIntent;
editIntent = new Intent(PetDetailTabbedActivity.this, EditPetActivity.class);
editIntent.putExtra("PetID", iPetID);
startActivityForResult(editIntent, REQUEST_CODE);
}
private void deletePet() {
//bundle up the iEntryID for the dialog to pass back;
// otherwise it's null when the dlg closes
Bundle args = new Bundle();
args.putInt("ItemID", iPetID);
DeleteConfirmDialog dlg = new DeleteConfirmDialog();
dlg.setArguments(args);
dlg.show(getSupportFragmentManager(), "Confirm Delete Dialog");
}
@Override
public void applyValue(int itemid) {
//the deletePet method in the db helper class also deletes journal entries for this pet
Integer iPetID = (Integer)itemid;
boolean bSuccess = myDB.deletePet(iPetID);
if (bSuccess == true) {
displayToastShort("Successfully deleted");
Intent gobacktoprevactivity = new Intent();
setResult(RESULT_OK,gobacktoprevactivity);
finish();
}
else
displayToastLong("Cannot delete the pet");
}
public void displayToastLong(String message) {
StyleableToast.makeText(this, message, Toast.LENGTH_LONG, R.style.AccentToast).show();
}
public void displayToastShort(String message) {
StyleableToast.makeText(this, message, Toast.LENGTH_SHORT, R.style.AccentToast).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
recreate();
journalFrag.adapter.notifyDataSetChanged();
}
}
}
}
更新:这是部分工作的布局(显示按钮)。正如您所看到的,当ListView中有少量行时,它会从底部浮动,当有多行时会向下推。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<ListView
android:id="@+id/journallistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="3"/>
<LinearLayout
android:id="@+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/addentrybtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/addentrybtn" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
将此内容写入LinearLayout标记
android:layout_above="@+id/addentrybtn"
它会将您的列表视图放在底部的按钮上方。
答案 1 :(得分:-1)
<强>更新强>
在重新阅读您的问题后,我设置了一个项目,该项目使用带有两个片段的活动,如上所述。
这是JournalFragment布局(上一个答案中建议的布局):
<?xml version="1.0" encoding="utf-8"?>
<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" >
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/addentrybtn"
android:orientation="vertical"
android:padding="8dp"
>
<ListView
android:id="@+id/journallistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/addentrybtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:text="ADD ENTRY" />
</RelativeLayout>
使用JournalFragment的代码:
public class JournalFragment extends Fragment {
public static JournalFragment newInstance(){
return new JournalFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_journal, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView listView = view.findViewById(R.id.journallistview);
final ListAdapter listAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, getItems(100));
listView.setAdapter(listAdapter);
}
private String [] getItems(int count){
String [] items = new String[count];
for(int i = 0; i < count; i++){
items[i] = "Item "+ i;
}
return items;
}
}
DetailFragment在这里没用(为了显示目的而存根)但为了完整性我将包含它:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:layout_centerInParent="true"
android:text="Detail Fragment!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
DetailFragment的代码(再次为了完整性):
public class DetailFragment extends Fragment {
public static DetailFragment newInsance(){
return new DetailFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_detail, container, false);
}
}
这是Activity布局(准系统RelativeLayout,Toolbar,TabLayout和ViewPager):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/journalToolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/journalToolbar"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:tabTextColor="#fff"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs" />
</RelativeLayout>
活动代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.journalToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewPager viewPager = findViewById(R.id.viewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
}
private final class PagerAdapter extends FragmentPagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return position == 0 ? getString(R.string.journal) : getString(R.string.details);
}
@Override
public Fragment getItem(int position) {
if(position == 0){
return JournalFragment.newInstance();
}else{
return DetailFragment.newInsance();
}
}
@Override
public int getCount() {
return 2;
}
}
}
祝你好运,快乐的编码!