gpt4 book ai didi

android - 动态设置 LinearLayout 的宽度?

转载 作者:行者123 更新时间:2023-11-29 18:07:22 32 4
gpt4 key购买 nike

我想制作自己的自定义“进度条”。我通过绘制线性布局来做到这一点,每个布局都有不同的颜色。之后我想为它们每个分配一个宽度,使其看起来像一个进度条。我现在拥有的东西:

我的 CustomAdapter 项目的 XML 文件(在 Gridview 中):

<?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"
android:background="#33c2bd" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineScore"
android:layout_width="20dp"
android:layout_height="2dp"
android:background="#eef05e"
android:paddingLeft="20dp"
android:paddingTop="0dp"
android:paddingBottom="20dp"
android:layout_below="@+id/tvLevelScore"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineScoreTotal"
android:layout_width="20dp"
android:layout_height="2dp"
android:background="#0d7975"
android:paddingRight="20dp"
android:paddingTop="0dp"
android:paddingBottom="20dp"
android:layout_below="@+id/tvLevelScore"
android:layout_toRightOf="@+id/lineScore"/>

</RelativeLayout>

然后在 getView 方法下的 CustomAdapter 类中,我尝试将 lineScoreTotal 设置为项目宽度的 80%:

double viewWidth = (double) mView.getWidth();
int widthScoreBar = (int) (viewWidth * 0.8);
LinearLayout ln = (LinearLayout) mView.findViewById(R.id.lineScoreTotal);
ln.layout(0, 2, widthScoreBar, -1);

但是,它什么也没做……我是不是应用了错误的代码来设置宽度?或者我使用 LinearLayout 绘制那些“条形图”的想法可能是错误的做法?

编辑 getView 方法:

@Override
public View getView(int position, View v, ViewGroup parent) {
View mView = v;

if (mView == null) {

LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(R.layout.levelselector_item, null);

}

TextView level = (TextView) mView.findViewById(R.id.tvLevel);
TextView levelScore = (TextView) mView.findViewById(R.id.tvLevelScore);

if (mView != null) {

level.setText(Integer.toString(getItem(position).getLevel()));

loadDataBase();
int correctAnswers = myDbHelper.getCorrectAnswers(getItem(position).getLevel());
correctAnswersPrev = 0;
correctAnswersPrev = myDbHelper.getCorrectAnswersPrev(getItem(position).getLevel());

String correct = Integer.toString(correctAnswers);

levelScore.setText(correct + "/60");
level.setTypeface(font);
levelScore.setTypeface(font);

LinearLayout ln = (LinearLayout) mView.findViewById(R.id.lineScoreTotal);
ln.setLayoutParams(new FrameLayout.LayoutParams(10, 10));
}
return mView;
}

最佳答案

试试看

没有重力

ln.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

或重力

ln.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1f));

关于android - 动态设置 LinearLayout 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705846/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com