1
我在嘗試使用展開/摺疊動畫設置線性佈局的高度時遇到了一些問題。下面是我的XML:Android設置高度
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnNewsFeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/lightred"
android:minHeight="20dp"
android:paddingBottom="5dp"
android:text="News feed"
android:textColor="#FFFFFF"
android:textSize="13dp" />
<LinearLayout
android:id="@+id/llNewsFeed"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/btnNewsFeed"
android:background="#FFFFFF"
android:orientation="vertical"
android:visibility="gone" >
<ListView
android:id="@+id/EventNewsFeedListview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="#F6CECE" >
</ListView>
</LinearLayout>
</RelativeLayout>
而當我的按鈕的onClick,它要麼執行擴大()或COLAPSE():
btnNewsFeed.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (llNewsFeed.getVisibility() == View.GONE) {
expand();
} else {
collapse();
}
});
private void expand() {
llNewsFeed.setVisibility(View.VISIBLE);
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
llNewsFeed.measure(widthSpec, heightSpec);
ValueAnimator mAnimator = slideAnimator(0,
llNewsFeed.getMeasuredHeight());
mAnimator.start();
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llNewsFeed
.getLayoutParams();
layoutParams.height = value;
llNewsFeed.setLayoutParams(layoutParams);
}
});
return animator;
}
private void collapse() {
int finalHeight = llNewsFeed.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() {
public void onAnimationEnd(Animator animator) {
llNewsFeed.setVisibility(View.GONE);
}
public void onAnimationCancel(Animator arg0) {
}
public void onAnimationRepeat(Animator arg0) {
}
public void onAnimationStart(Animator arg0) {
}
});
mAnimator.start();
}
但我發現了這樣的輸出:
新聞饋送的高度太小了。我想知道是否有任何方法將它設置爲填充父項?
在此先感謝。
任何想法?因爲我不確定哪個部分出了問題。在llNewsFeed內部,我有一個滾動條上下滾動。但我希望它填補父母 – hyperfkcb 2014-12-03 13:53:06