【问题标题】:Setting width of LinearLayout dynamically?动态设置LinearLayout的宽度?
【发布时间】:2014-03-30 21:33:29
【问题描述】:

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

我的 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;
    }

【问题讨论】:

    标签: android width android-linearlayout


    【解决方案1】:

    尝试任何一种方式

    没有重力

    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));
    

    【讨论】:

    • 谢谢,但我收到此错误:10-03 12:41:17.394: E/AndroidRuntime(27008): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
    • getWindowManager().getDefaultDisplay().getWidth() //试试这个方法计算宽度
    • 您认为这是导致错误的原因吗?假设我只输入 ln.setLayoutParams(new LinearLayout.LayoutParams(10, 10));我仍然收到错误...
    • 可以粘贴getview方法吗?
    • 我编辑了我最初的问题并将 getview 方法放在那里。
    【解决方案2】:
    ln.setLayoutParams(new
           LayoutParams(widthScoreBar,LayoutParams.WRAP_CONTENT));
    

    尝试使用 LayoutParams 进行设置。

    【讨论】:

    • 也谢谢你,但我遇到了与上面相同的错误:10-03 12:41:17.394: E/AndroidRuntime(27008): java.lang.ClassCastException: android.view.ViewGroup $LayoutParams
    • 你能粘贴你的堆栈跟踪吗?
    • 我希望您的意思是:10-03 12:50:24.121:E/AndroidRuntime(27724):致命异常:主要 10-03 12:50:24.121:E/AndroidRuntime(27724): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams 10-03 12:50:24.121: E/AndroidRuntime(27724): 在 android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1303) 10-03 12 :50:24.121: E/AndroidRuntime(27724): 在 android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1250) 10-03 12:50:24.121: E/AndroidRuntime(27724): 在 android.widget。 RelativeLayout.sortChildren(RelativeLayout.java:281)
    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2016-04-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 2011-11-26
    • 2022-11-10
    相关资源
    最近更新 更多