以线性布局分隔(给出边距)文本视图

时间:2017-11-01 10:29:29

标签: android android-xml

我在TextView内有一个Linearlayout的布局,在从微调器中选择一个项目时会膨胀。

结果如下:

enter image description here

我希望它是这样的:

enter image description here

这是我的tag_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/tag_background"
    android:gravity="center"
    android:layout_marginLeft="5dp"
    android:paddingBottom="6dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="6dp">

        <TextView
            android:id="@+id/chip_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="#Tag2"
            android:textColor="@android:color/white"
            android:textSize="14sp" />

</LinearLayout>

这是我的线性布局,其中该视图将被夸大:

<LinearLayout
    android:id="@+id/tag_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/tag_background"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_below="@+id/issuesStrengthsBrandspinner"
    android:paddingBottom="4dp"
    android:layout_marginLeft="20dp"
    android:paddingTop="4dp" />

这是我的背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/colorPrimaryDark"/>
    <corners android:radius="20dp"/>
</shape>

这就是我提出观点的方式:

        LinearLayout linearLayout = (LinearLayout) dialog2.findViewById(R.id.tag_container);

        for (HashMap.Entry<Integer, String> entry : issuesStrengthsbrandNameIDMap.entrySet()){
            View view2 = getLayoutInflater().inflate(R.layout.tag_layout, null);
            Log.i("NAME", "onItemClick: "+entry.getValue());
            ((TextView) view2.findViewById(R.id.chip_text)).setText(entry.getValue());
            linearLayout.addView(view2);
        }

3 个答案:

答案 0 :(得分:1)

以编程方式尝试此设置保证金

for (HashMap.Entry<Integer, String> entry : issuesStrengthsbrandNameIDMap.entrySet()){
     View view2 = getLayoutInflater().inflate(R.layout.tag_layout, null);
     Log.i("NAME", "onItemClick: "+entry.getValue());
     TextView tv=  (TextView) view2.findViewById(R.id.chip_text)).setText(entry.getValue();
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     params.setMargins(10,0,10,0); //setMargins(left, top, right, bottom);
     tv.setLayoutParams(params);
     linearLayout.addView(tv);
  }

答案 1 :(得分:0)

TextView

提供保证金
android:marginLeft="8dp"
android:marginRight="8dp"

例如

<TextView
    android:id="@+id/chip_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:marginLeft="8dp"
    android:marginRight="8dp"
    android:text="#Tag2"
    android:textColor="@android:color/white"
    android:textSize="14sp" />

答案 2 :(得分:0)

从根线性布局中删除背景属性并添加文本视图。这将创建单独的文本。

<!--Initializing Push Notification-->

const push = PushNotification.init({
    android: {
        senderID: "12345....."  // the same without this line
    },
    browser: {
        pushServiceURL: 'http://push.api.phonegap.com/v1/push'
    },
    ios: {
        alert: "true",
        badge: true,
        sound: 'false'
    },
    windows: {}
});            

push.on('registration', function(data) {
    obj.regid = data.registrationId;
    alert(data.registrationId);   // no alert in built app
});

快乐的编码!!