我尝试使用getLayoutParams()
将片段中的视图与其父AppCompatActivity
中的视图对齐
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewImAligning.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.video_view_from_appCompat);
viewImAligning.setLayoutParams(params);
我正确地在片段上初始化R.id.video_view_from_appCompat
,因为我可以从Fragment的setUserVisibleHint
方法获得它的宽度/高度等。
但是,无论我在片段的生命周期中放置viewImAligning
video_view_from_appCompat
与viewImAligning.setLayoutParams(params);
对齐
目前,我正在使用getLocationsOnScreen
int [] generatedLocations = new int[2];
locations = generatedLocations;
videoViewFromAppCompat.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
viewImAligning.setY(y);
虽然hacky解决方法可以解决问题,但我想了解为什么我没有让getLayoutParams工作。
编辑:代码和XML(含有AsyncTask
的意大利面条代码包括与它有关的内容
public abstract class VideoIntro extends AppCompatActivity{
private LinearLayout videoViewFromAppCompat;
@Override
final protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.video_intro_layout_test);
new LoadContestTask().execute();
}
private class LoadContestTask extends AsyncTask<Object, Object, Object> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
...
}
@Override
protected Object doInBackground(Object... params) {
initialize();
return true;
}
protected void onPostExecute(Object result) {
loadContent();
pDialog.dismiss();
}
}
private void initialize() {
...
videoViewFromAppCompat = (LinearLayout) findViewById(video_view_from_appCompat);
}
...
}
video_intro_layout_test
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<SurfaceView
android:layout_width="0px"
android:layout_height="0px" />
<LinearLayout
android:id="@+id/video_view_from_appCompat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/top"
android:paddingLeft="45dp"
android:paddingRight="45dp"
android:paddingTop="36dp"
android:visibility="invisible">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<com.my_test.app.CustomViewPager
android:id="@+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.my_test.app.CustomViewPager>
片段代码
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
videoViewFromAppCompat = (LinearLayout) getActivity().findViewById(R.id.video_view_from_appCompat);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
viewImAligning = (RelativeLayout) v.findViewById(R.id.view_im_aligning);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser )
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewImAligning.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.video_view_from_appCompat);
viewImAligning.setLayoutParams(params);
答案 0 :(得分:1)
我想我明白了。您正尝试将作为Fragment
(R.id.view_im_aligning
)基本视图的视图与R.id.video_view_from_appCompat
内属于您{{1}的视图(RelativeLayout
)对齐}}。问题是它们不共享同一个父级,这就是导致代码失败的原因。
我建议如下 - 如果您需要在video_view_from_appCompat下方显示专用片段,请添加Activity
以包含片段,将片段添加到该视图中。之后,您可以将FrameLayout对齐到video_view_from_appCompat。
所以你的xml看起来有点像:
FrameLayout