【发布时间】:2014-01-25 18:50:31
【问题描述】:
我正在尝试创建一个动态布局一系列自定义视图的片段。此布局的主要内容是嵌套在 LinearLayout 中的 RelativeLayout(使其水平居中),嵌套在 ScrollView 中。
RelativeLayout 有几个 TextView 和一个 9 补丁 ImageView,旨在随动态添加的自定义视图进行缩放。但是,图像(下面的achievements_bgImageView)最终成为屏幕的大小,并且即使在我添加了适当数量的自定义视图之后也不尊重其父RelativeLayout 的大小。当我手动设置results_mainLayout 的大小时,图像可以很好地缩放(请参阅下面注释掉的行),但如果我尝试让RelativeLayout 的wrap_content 处理它自己的大小,则不会执行任何操作。
ScrollView 尊重 RelativeLayout 的大小,因为所有内容都存在,只是 imageView 没有拉伸以匹配此时的内容。
任何帮助将不胜感激...我的手动计算似乎不足以考虑不同的设备,尽管事实上我正在考虑屏幕密度并且我手动将 RelativeLayout 强制为恒定宽度.
值得注意的是,RelativeLayout 的测量大小始终等于屏幕的高度,无论其内容的总和是否大于或小于该高度。所以,本质上, WRAP_CONTENT 根本没有做它应该做的事情。我没有引用 RelativeLayout 的任何边缘,所以循环依赖应该不是问题。
fragment_achievements.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<RelativeLayout
android:layout_width="320dp"
android:layout_height="wrap_content"
android:id="@+id/achievements_mainLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/achievements_bgImageView"
android:src="@drawable/bkg_achievements9"
android:adjustViewBounds="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name Field"
android:id="@+id/achievements_nameTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="30dp"/>
<ImageView
android:layout_width="52dp"
android:layout_height="52dp"
android:id="@+id/achievements_avatarImageView"
android:layout_below="@+id/achievements_nameTextView"
android:layout_alignLeft="@+id/achievements_nameTextView"
android:src="@drawable/achieve_avatar"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Top Moment:"
android:id="@+id/textView2"
android:layout_alignBottom="@+id/achievements_avatarImageView"
android:layout_toRightOf="@+id/achievements_avatarImageView"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:textSize="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Me Overall:"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_marginTop="16dp"
android:textSize="12dp"/>
<TextView
android:layout_width="52dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="153"
android:id="@+id/achievements_totalPointsTextView"
android:gravity="center"
android:layout_alignTop="@+id/achievements_avatarImageView"
android:layout_alignRight="@+id/achievements_bgImageView"
android:layout_alignEnd="@+id/achievements_bgImageView"
android:layout_marginRight="31dp"
android:textColor="#f7a033"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Moment"
android:id="@+id/achievements_topMomentTextView"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_marginLeft="5dp"
android:textSize="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="153"
android:id="@+id/achievements_overallTextView"
android:layout_alignTop="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:layout_marginLeft="5dp"
android:textSize="12dp"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
AchievementFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = null;
fragmentView = inflater.inflate(R.layout.fragment_achievements, container, false);
ImageView avatarImageView = (ImageView)fragmentView.findViewById(R.id.achievements_avatarImageView);
...
// Basic Achievement List Setup
RelativeLayout mainLayout = (RelativeLayout)fragmentView.findViewById(R.id.achievements_mainLayout);
AchievementRow currentRow = null;
List achievementTypeList = CampaignManager.sharedManager().sortedAchievementTypeList();
int achievementCount = achievementTypeList.size();
for (int i = 0; i < achievementCount; i++) {
AchievementType achievementType = (AchievementType)achievementTypeList.get(i);
// Every third achievement creates a new row.
if ((i % 3) == 0) {
AchievementRow row = (AchievementRow)inflater.inflate(R.layout.widget_achievementrow, null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (currentRow == null) {
layoutParams.addRule(RelativeLayout.BELOW, avatarImageView.getId());
layoutParams.setMargins(10, 70, 10, 0);
} else {
layoutParams.addRule(RelativeLayout.BELOW, currentRow.getId());
layoutParams.setMargins(10, 10, 10, 0);
}
layoutParams.addRule(RelativeLayout.ALIGN_LEFT, backgroundImageView.getId());
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, backgroundImageView.getId());
row.setLayoutParams(layoutParams);
row.setId(i+1);
mainLayout.addView(row);
currentRow = row;
}
// Now setup the Button
AchievementButton achievementButton = currentRow.buttonForIndex(i % 3);
achievementButton.achievementType = achievementType;
achievementButton.setOnClickListener(achievementButtonListener);
achievementButton.setVisibility(View.VISIBLE);
CacheManager.sharedManager().fetchAchievementThumbnail(getActivity(), achievementButton, achievementType);
}
// This is the manual scaling of mainLayout
// float scale = getResources().getDisplayMetrics().density;
// float headerHeight = scale * 150.0f;
// float rowHeight = scale * 78.0f;
// ViewGroup.LayoutParams mainLayoutParams = mainLayout.getLayoutParams();
// mainLayoutParams.height = (int)(headerHeight + (Math.ceil(achievementCount / 3.0) * rowHeight));
return fragmentView;
}
【问题讨论】:
-
首先,您可能应该为此使用列表视图或网格视图。其次,布局参数在下一次布局传递之后才会应用。您是否尝试过调用 invalidate() 或 requestLayout()?
-
invalidate()、requestLayout() 和 forceLayout() 似乎都什么都不做......我不明白的是这一切还在 onCreateView 中,没有任何东西被绘制或布局添加所有这些视图的时间。