[英]How to modify the height and width of a LinearLayout programmatically for a customAlertDialog?
[英]How to change the size (width and height) of progressbar to fit all in linearlayout?
我想向线性布局添加未知数量的进度条。 (如 Whatsapp 状态和 Instagram 故事)我可以将进度条添加到线性布局,但进度条只显示最多 7 个进度条。
到目前为止我的代码:
while (i < 10){
i ++;
View child = getLayoutInflater().inflate(R.layout.stories_progress, null);
linearLayout.addView(child);
}
我想以编程方式缩小进度条的大小,所以当我有 2 个进度条时,它们会以 50%/50% 填充布局,当 5、10 甚至 20 个进度条时,它们会变小,它们都会显示在线性布局我试图获取线性布局的当前宽度并除以进度条的数量,但线性布局的宽度总是返回0;
我的 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_layout_id"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
tools:context=".stories">
</LinearLayout>
我的进度条
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ProgressBar
android:id="@+id/stories_progressBar_id"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ProgressBar>
</LinearLayout>
您可能没有等待顶部的LinearLayout具有大小。 您可以等待使用ViewTreeObserver.OnGlobalLayoutListener分配大小。 这是一些应该有帮助的代码:
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
linearLayout.removeAllViews();
int newWidth = linearLayout.getWidth() / 10;
int newHeight = linearLayout.getHeight();
int i = 0;
while (i < 10) {
i++;
LinearLayout child =
(LinearLayout) getLayoutInflater().inflate(R.layout.stories_progress,
linearLayout, false);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
lp.width = newWidth;
lp.height = newHeight;
linearLayout.addView(child);
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.