从右到左元素的方向在android 4.2以上



我的android应用程序中有一个表布局,希望第一列在右边,第二列在左边,我尝试使用android:layoutDirection="rtl",但它只适用于4.2及更高版本,在旧版本中,我可以用什么来设置从右到左的方向。

我的xml文件是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/form"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5" >
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/barbac"
        android:gravity="right" >
        <ImageView
            android:id="@+id/imgtit"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/noticetit"
            android:layout_marginBottom="6dp"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ajeer_logo_trans2" />
        <TextView
            android:id="@+id/noticetit"
            style="@style/textstl"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_toLeftOf="@+id/imgtit"
            android:text="@string/worknotice" />
    </RelativeLayout>
    <TableLayout
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="60dp"
        android:layoutDirection="rtl"
         >
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" 
           >

 <TextView
                android:id="@+id/laborname"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/laborname" 
                />
            <TextView
                android:id="@+id/Vlaborname"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/iqamanum"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/iqamanum" />
            <TextView
                android:id="@+id/Viqamanum"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/instname1"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instname1" />
            <TextView
                android:id="@+id/Vinstname1"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
            </TextView>
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/instnum"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instnum" />
            <TextView
                android:id="@+id/Vinstnum"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/instactive"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instactive" />
            <TextView
                android:id="@+id/Vinstactive"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/instname2"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instname2" />
            <TextView
                android:id="@+id/Vinstname2"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/instnum2"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/instnum2" />
            <TextView
                android:id="@+id/Vinstnum2"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/sdate"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/sdate" />
            <TextView
                android:id="@+id/Vsdate"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/edate"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:text="@string/edate" />
            <TextView
                android:id="@+id/Vedate"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
            <TextView
                android:id="@+id/lacation"
                style="@style/noticform"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/location" />
            <TextView
                android:id="@+id/Vlocation"
                style="@style/noticformstl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>
        <TableRow
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_marginBottom="80dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="@drawable/roundrectangle" >
            <TextView
                android:id="@+id/expdate"
                style="@style/expdatestl"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="30dp"
                android:layout_marginTop="9dp"
                android:layout_weight="1"
                android:text="@string/expdate" />
            <TextView
                android:id="@+id/vexpdate"
                style="@style/expdatestl1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginTop="9dp"
                android:layout_weight="1" />
        </TableRow>
    </TableLayout>
<TextView 
    android:id="@+id/copyrights"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:gravity="center"
      android:layout_alignParentBottom="true"
android:text="@string/copyrights"    />
</RelativeLayout>

提前感谢

LayoutDirection是在API v17中添加的一个新属性。在此之前,这种任务是使用针对每个特定小区的gravitylayout_gravity来完成的。我不记得是哪一个了。

对我来说,最终起作用的是Web视图。因为那时我只是用CSS来设计我的文本。如果你想让onTouchListener在点击时做点什么,你还需要一个onTouch监听器

将TextView更改为Webview:

<WebView
        android:id="@+id/noticetit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

我的代码:

WebView webView = (WebView) findViewById(R.id.noticetit);
private void setWebView() {
            String htmlcss = "<html lang="he"><head><meta charset="utf-8" />"
                    + "<style type="text/css">.rtl {direction: rtl;color:black;font:20px arial;}</style></head>"
                    + "<body class="rtl"><div class="rtl">" + webView
                    + "</div></body></html>";
            webView.loadDataWithBaseURL(null, htmlcss, "text/html", "utf-8", null);
            webView.setOnTouchListener(new OnTouchListener() {
                // Removed @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (v.getId() == R.id.maintitle
                            && event.getAction() == MotionEvent.ACTION_DOWN) {
                        //Put your code here
                    }
                    return false;
                }
            });
        }

希望这能帮到你!

相关内容

最新更新