如何在列表视图中创建一条线取决于百分比值

时间:2014-12-18 12:06:25

标签: android android-layout android-activity

enter image description here我创建了视图并设置了宽度大小为百分比,但如果百分比为100,则视图未完全占用,如果您有任何不同的视图,请告诉我解决方案吗?更新问题。 enter image description here

![我这样的确切屏幕] ![我的屏幕如果百分比100%表示蓝线显示如此,屏幕54.34%所以蓝线只有一半线![我的屏幕如果百分比100%表示蓝线显示如此,屏幕54.34 %所以蓝线只有一半的线] 我的屏幕如果百分比100%表示蓝线显示如此,屏幕54.34%所以蓝线的一半只有My screen the if percentage 100% means the blue line shows like this, the screen 54.34% so the blue line half of the line only[3]

//查看布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fafafa"
    android:orientation="vertical"
    tools:context=".DashboardV4MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#BDBDBD" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:paddingLeft="3dp" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="2"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:paddingRight="1dp" >

            <TextView
                android:id="@+id/db_lob"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center_vertical"
                android:paddingLeft="5dp"
                android:text="Own"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/db_lob_percentage"
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:gravity="center_vertical"
                android:paddingLeft="5dp"
                android:text="100%"
                android:textColor="@color/black"
                android:textSize="10sp" />

            <View
                android:id="@+id/db_lob_percent"
                android:layout_width="wrap_content"
                android:layout_height="4dp"
                android:layout_marginLeft="5dp"
                android:background="@color/blue2" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="0.5"
            android:background="#FFFFFF"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/db_lob_count"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="85"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="0.5"
            android:background="#FFFFFF"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/navigation_next_item" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#BDBDBD" />
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用此代码查找屏幕高度和宽度。

    final LinearLayout layout_linearLayout = (LinearLayout) findViewById(R.id.layout_linearLayout);
    layout_linearLayout.post(new Runnable() {
        public void run() {
            int height = layout_linearLayout.getHeight();
            int width = layout_linearLayout.getWidth();

            Log.e("Height", height + "");

            MySharedPref.getInstance(Login.this).setString(
                    "sendScreenHeight_SPLASHSCREEN", height + "");
            MySharedPref.getInstance(Login.this).setString(
                    "sendScreenWidth_SPLASHSCREEN", width + "");

            setWidth_Percent()
        }
    });

之后,设置布局的宽度和高度,如下所示:

public void setWidth_Percent(){
    View db_lob_percent = (View) findViewById(R.id.db_lob_percent);

    layoutDialog.setLayoutParams(new FrameLayout.LayoutParams(dialogWidth*3/4+dialogWidth*1/8,LayoutParams.WRAP_CONTENT));
}

修改

    You have width for Green and purple box reduce this width from parent width by using 
this function . pass your width(dp) in this function it will return you the width(pixels)


public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}

- 已编辑2:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fafafa"
    android:orientation="vertical"
    tools:context=".DashboardV4MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone" >

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#BDBDBD" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:paddingLeft="3dp" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#FFFFFF"
                android:orientation="vertical"
                android:paddingRight="1dp" >

                <TextView
                    android:id="@+id/db_lob"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center_vertical"
                    android:paddingLeft="5dp"
                    android:text="Own"
                    android:textColor="#000000" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="#FFFFFF"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/db_lob_count"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:text="85"
                    android:textColor="#000000" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="#FFFFFF"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#BDBDBD" />
        </LinearLayout>
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/db_lob_percentage"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:gravity="center_vertical"
            android:paddingLeft="5dp"
            android:text="100%"
            android:textColor="#000000"
            android:textSize="10sp" />

        <View
            android:id="@+id/db_lob_percent"
            android:layout_width="wrap_content"
            android:layout_height="4dp"
            android:layout_marginLeft="5dp"
            android:background="#000fff" />
    </FrameLayout>

</FrameLayout>