动态排列TextView的边距

时间:2014-11-12 13:53:06

标签: android textview

我正在使用自定义阵列适配器。我正在使用扩展的textview,如果文本更大,它会缩小。我的listview项目布局如下

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="3" >

<LinearLayout
    android:id="@+id/textLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.8"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/textLabelLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1.0"
        android:orientation="vertical" >

        <ft.mobile.client.android.widget.AutoResizeTextView
            android:id="@+id/accountNameLabel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:includeFontPadding="false"
            android:maxLines="1"
            android:text="ROPL-12345111000"
            custom:dataType="account"
            custom:maxLineCount="2"
            custom:minFontSize="10"
            custom:preferredLineCount="1"
            custom:shrinkTextToFit="true" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/textValueLayout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="05dp"
        android:layout_marginRight="05dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1.2"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/accountBalanceLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:drawableRight="@drawable/drill_down"
            android:ellipsize="none"
            android:singleLine="false"
            android:text="Amount"
            android:textColor="@color/black"
            android:textSize="14dp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

问题是,当第一个文本视图较大时,它会覆盖文本视图前面的textview。我只是想动态地为texview提供保证金,以便它位于底部

3 个答案:

答案 0 :(得分:0)

只需使用LayoutParams即可:

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);

根据您使用的布局,您应使用RelativeLayout.LayoutParamsLinearLayout.LayoutParams

答案 1 :(得分:0)

你可以这样做:

TextView tv = (TextView)findViewById(R.id.textview);
LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textViewLayoutParams.setMargins(50, 10, 0, 0);
tv.setLayoutParams(buttonLayoutParams);

答案 2 :(得分:0)

试试这个修改过的xml文件。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="horizontal"
    android:weightSum="3" >

        <ft.mobile.client.android.widget.AutoResizeTextView
                android:id="@+id/accountNameLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=1.8
                android:includeFontPadding="false"
                android:maxLines="1"
                android:text="ROPL-12345111000"
                custom:dataType="account"
                custom:maxLineCount="2"
                custom:minFontSize="10"
                custom:preferredLineCount="1"
                custom:shrinkTextToFit="true" />


            <TextView
                android:id="@+id/accountBalanceLabel"
                android:layout_width="wrap_content"
                android:layout_weight=1.2
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:drawableRight="@drawable/drill_down"
                android:ellipsize="none"
                android:singleLine="false"
                android:text="Amount"
                android:textColor="@color/black"
                android:textSize="14dp"
                android:textStyle="bold" />
    </LinearLayout>