我有一个Activity
,在创建时加载FrameLayout
包含SearchView
和Button
。按钮的onClick
方法会在主ListFragment
之上加载自定义FrameLayout
。
我覆盖onCreate
的{{1}}方法以加载我的自定义ListFragment
,并使用我手动创建的字符串数组中的值填充列表。所有这些都能正常工作,但我想在屏幕底部加载ArrayAdapter
。
我尝试将主ListFragment
的重力更改为底部,因为它是FrameLayout
的父级,但这会使ListFragment
和SearchView
降为屏幕底部以及Button
。
在这个例子中,我在列表中加载了三行。如何向下移动ListFragment
,以便屏幕底部只显示第一行,其下方有两行(屏幕外)?
我的MapActivity是主ListFragment
Activity
MapActivity的布局
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_map, container, false);
}
}
// Define the onShowTimesButtonClick listener
public void onShowTimesButtonClick(View view){
// Instantiate timeListFragment
TimeListFragment timeListFragment = new TimeListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.container, timeListFragment, "timeList");
transaction.commit();
}
}
TimeListFragment是我的自定义<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity"
tools:ignore="MergeRootFrame"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false">
<SearchView
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/searchBar"
android:layout_gravity="start|top"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:background="#656565"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text" />
<Button
android:id="@+id/timesButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top|center"
android:layout_marginTop="90dp"
android:text="@string/times_button"
android:onClick="onShowTimesButtonClick"/>
</FrameLayout>
。
ListFragment
TimeListFragment的布局
import android.app.ListFragment;
import android.os.Bundle;
import android.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
/**
* A simple {@link Fragment} subclass.
*/
public class TimeListFragment extends ListFragment {
public TimeListFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Temporary time data
String[] times = new String[]{"1:00", "0:30", "0:45"};
// Load the time data
setListAdapter(new TimesListArrayAdapter(getActivity(), R.layout.layout_times_list_row, R.id.listText, times));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_times_list, container, false);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
container.setLayoutParams(layoutParams);
return view;
}
}
答案 0 :(得分:0)
这是我到目前为止所拥有的。我可以通过将重力设置为底部将ListFragment
移动到屏幕底部。在TimeListFragment的onCreateView
方法中,我将layoutParams
应用于容器;根据Android文档,此容器是片段的UI附加到的父视图。为了将layoutParams
应用于片段本身,我必须首先通过调用膨胀视图上的findViewById
来查找片段视图。
onCreateView
的正确代码如下所示
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_times_list, container, false);
View fragmentView = view.findViewById(R.id.timesList);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
fragmentView.setLayoutParams(layoutParams);
return view;
}