这是我的Activity布局。我想动态地在屏幕中间显示android.widget.ProgressBar
。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:id="@+id/rlSearchToolContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:gravity="bottom"
android:layout_gravity="bottom"
android:visibility="gone">
<LinearLayout
android:id="@+id/llSearchComponentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
android:layout_toLeftOf="@+id/btnDone"
android:gravity="bottom"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="@+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="@+id/frameLeftSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
现在在FrameLayout(id = frameContainer)中我替换了一个BaseFragment片段。(fragment_base.xml)
MainActivity.java
class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BaseFragment mFileListFragment = new BaseFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameContainer, mFileListFragment);
transaction.commit();
}
}
fragment_base.xml
<?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">
<FrameLayout
android:id="@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</LinearLayout>
BaseFragment.java
public class BaseFragment extends Fragment{
private View mView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_phone_file_base, null);
setLayoutViews(mView);
}
return mView;
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FileListFragment mFileListFragment = new FileListFragment();
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameContainer, mFileListFragment);
transaction.commit();
}
}
现在在该BaseFragment的FrameLayout(id = frameContainer)中,我打开了FileListFragment的子片段。 (fragment_file_list.xml)
fragment_file_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rvFileList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
</RelativeLayout>
FileListFragment.java
public class FileListFragment extends Fragment{
private View mView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_file_list, null);
setLayoutViews(mView);
}
return mView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Here there is a server call which retrieves File Data from server. When I send server request I start progressBar and after response I hide progressBar programatically.
ProgressBar mProgressBar = new ProgressBar(activity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mProgressBar.setLayoutParams(params);
mProgressBar.getIndeterminateDrawable().setColorFilter(activity.getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
FrameLayout listFrame = getParentFragment().getView().findViewById(R.id.frameContainer);
if(listFrame != null) {
listFrame.addView(mProgressBar);
}
}
}
但它没有在屏幕中间显示progressBar。它显示在屏幕中间的下方,如下图所示。
答案 0 :(得分:1)
您可以尝试 android:layout_gravity
儿童向其父母提供的标准重力常数。
您可以通过设置子视图的 layout_gravity 来将视图对齐在 Framelayout 的中心。
<强> android:layout_gravity="center"
强>
将对象放在其容器的中心位置,不要改变其大小。
答案 1 :(得分:0)
这是因为你在framelayout的中心添加进度条,试试这个
<android.support.design.widget.CoordinatorLayout
android:id="@+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:id="@+id/rlSearchToolContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:gravity="bottom"
android:layout_gravity="bottom"
android:visibility="gone">
<LinearLayout
android:id="@+id/llSearchComponentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
android:layout_toLeftOf="@+id/btnDone"
android:gravity="bottom"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="@+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="@+id/frameLeftSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
答案 2 :(得分:0)
使用Relativelayout尝试使用Relativelayout作为进度条的容器,并使用centerinparent属性将进度条置于屏幕中间:
RelativeLayout layout = new RelativeLayout(this);
progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
setContentView(layout);