我在父视图中心的现有视图的左侧添加了一个视图。我希望布局更新并将我动态添加到现有视图左侧的视图和现有视图居中。
我之前查看了this question并尝试在添加了无法使用的视图后获取wrap_content视图:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, timePicker.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
picker.setLayoutParams(params);
lytBottom.addView(picker);
RelativeLayout.LayoutParams existingParams = (RelativeLayout.LayoutParams) lytBottom.getLayoutParams();
existingParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
并尝试按照建议的最高评论更改可见性。以上所有内容都是原始视图周围的wrap_content,完全忽略了动态添加的视图。
我是否有另一种方法可以接近这个以实现新视图和旧视图的居中,同时将新视图保留在旧视图的左侧?
xml:
<android.support.percent.PercentRelativeLayout
android:id="@+id/lytBottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/lytTop"
android:layout_centerHorizontal="true"
app:layout_heightPercent="30%">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:timePickerMode="spinner"/>
</android.support.percent.PercentRelativeLayout>